0
votes

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>

}
1
It seems like MultipartFormData should be MultipartFormData[something], similar to how you have Option[Long]. That's what it means when it is referring to Type Parameters at least.James Whiteley
thanks for your comment, James. I thought too, but for example, neither [File] nor [FilePart] are passed, so stopped thinking about it...fststp
Why a the picName is of type MultipartFormData?o-0
thanks for comment, Dave. sorry, careless mistake. fixed it.fststp
i knew that TemporaryFile is default type then modified to MultipartFormData[TemporaryFile] but error "not found: value TemporaryFile" occured. github.com/playframework/…fststp

1 Answers

0
votes

solved.

when modified to MultipartFormData[_] , type mismatch;

[error] found : Option[play.api.mvc.MultipartFormData.FilePart[java.io.File]]

[error] required: play.api.mvc.MultipartFormData[_]

occured.

then i should write like this in views:

@(file: Option[MultipartFormData.FilePart[java.io.File]])