アップロードした画像の表示の仕方 / [ error ] class MultipartFormData takes type parameters
アップロードした画像をブラウザに表示したいので、(sihouetteのrequest.identityのように、)MultipartFormDataをviewに渡せば良いのではないかと思ったのですが、うまくいきません。
>class MultipartFormData takes type parameters
問題が起きているのはviewの最初の引数の宣言の部分です。
@(user: models.User, picName: MultipartFormData, fileOption: Option[Long])(implicit request: RequestHeader, messages: Messages, webJarAssets: WebJarAssets)
MultipartFormDataの型パラメータがどれに当たるのか、わかりません。そもそもMultipartFormDataを渡すべきなのかもわかりません。
(FileUploadController.scala)
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)))
}
}
(fileupload2.scala.html)
@(user: models.User, picName: 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>
}