0

I want to upload a file in my form. However, currently I only manage to do this separately.

def upload = Action(parse.multipartFormData) { request =>
request.body
      .file("picture")
      .map { picture =>
        // only get the last part of the filename
        // otherwise someone can send a path like ../../home/foo/bar.txt to write to other files on the system
        val filename    = Paths.get(picture.filename).getFileName
        val fileSize    = picture.fileSize
        val contentType = picture.contentType

  
        picture.ref.copyTo(Paths.get(s"pictures/$filename"), replace = true)
        Ok("File uploaded")
      }
      .getOrElse {
       ...
      }
  }

@helper.form(action = routes.PostController.upload, Symbol("enctype") -> "multipart/form-data") {
            @CSRF.formField
            <input type="file" name="picture">

            <p>
                <input type="submit">

            </p>
      }

Is there a way to add this to another form so that you can submit one form instead of having to submit an extra form for the upload?

Thanks in advance!

Dirac
  • 23
  • 4
  • [This](https://stackoverflow.com/questions/9452375/how-to-get-the-upload-file-with-other-inputs-in-play2) post is a bit old, but the pattern remains the similar. – Johny T Koshy Nov 25 '21 at 16:44

0 Answers0