user2975535 user2975535 - 9 months ago 99
Scala Question

Play Framework 2.5.9 Scala handling multipart/form-data file field allways Some

I'm following documentation from here to handle file upload in form.
My code is here:

def add(method: String) = Action(parse.multipartFormData) { implicit request =>
if (method == "GET") {
} else {
hasErrors => BadRequest(views.html.add(hasErrors)),
form => {

Whenever I submit this form, even if I didn't select file in file field I'm getting something like this in console:

[debug] application -

I can't understand why is this so. I think there are should be None in
if I didn't select file before submitting form.
How can I find if file was submitted?

Ok. Now I just copied code from documentation and got the same result :(
Here is my controller action:

def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
Ok("File uploaded")
}.getOrElse {
"error" -> "Missing file")

and here is my view:


@helper.form(action = routes.HomeController.upload, 'enctype -> "multipart/form-data") {

<input type="file" name="picture">

<input type="submit">


Whenever I submit this form I got "File uploaded" message.

Answer Source