I want to display the uploaded file(s) in the view; however I don't know how to do it.
first I tried to pass multipartFormData
to the views file but an error occurred:
class MultipartFormData takes type parameters
The problem is the following line:
@(user: models.User, file: MultipartFormData, fileOption: Option[Long])(implicit request: RequestHeader, messages: Messages, webJarAssets: WebJarAssets)
Within my controller class (FileUploadController.scala
) I have the following:
type FilePartHandler[A] = FileInfo => Accumulator[ByteString, FilePart[A]]
def handleFilePartAsFile: FilePartHandler[File] = {
case FileInfo(partName, filename, contentType) =>
val attr = PosixFilePermissions.asFileAttribute(util.EnumSet.of(OWNER_READ, OWNER_WRITE))
val path: Path = Files.createTempFile("multipartBody", "tempFile", attr)
if (Files.notExists(path)) Files.createFile(path)
val file = path.toFile
val fileSink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(path)
val accumulator: Accumulator[ByteString, IOResult] = Accumulator(fileSink)
accumulator.map {
case IOResult(count, status) =>
logger.info(s"count = $count, status = $status")
FilePart(partName, filename, contentType, file)
}
}
def upload = silhouette.SecuredAction.async(parse.multipartFormData(handleFilePartAsFile)) { implicit request =>
val fileOption = request.body.file("picName").map {
case FilePart(key, filename, contentType, file) =>
logger.info(s"key = ${key}, filename = ${filename}, contentType = ${contentType}, file = $file")
val data = operateOnTempFile(file)
FileUploadFormSupport.picsave(key, filename, contentType, file)
data
}
Future.successful(Ok(views.html.fileUpload2(request.identity, request.body.file("picName"), fileOption)))
}
}
Within my views file (fileupload2.scala.html
), I have the following:
@(user: models.User, file: MultipartFormData, fileOption: Option[Long])(implicit request: RequestHeader, messages: Messages, webJarAssets: WebJarAssets)
@import b3.inline.fieldConstructor
@main(Messages("fileupload.title"), Some(user)) {
<div class="user col-md-6 col-md-offset-3">
<div class="row data">
<div class="col-md-12">
<div class="row">
<img src="file">
<p class="col-md-6"><strong>@Messages("file.name"):</strong></p>
<p class="col-md-6">file size is @fileOption.getOrElse("None")</p>
</div>
</div>
</div>
</div>
}
MultipartFormData[something]
, similar to how you haveOption[Long]
. That's what it means when it is referring to Type Parameters at least.picName
is of typeMultipartFormData
?