Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play file upload form with additional fields

I have run into a problem with a Play 2.1.0 form that contains a file upload and an additional input field. I use

def uploadTaxonomy() = Action(parse.multipartFormData) {
    implicit request =>
      request.body.file("xml").map { file =>
        val xml = scala.io.Source.fromFile(file.ref.file).mkString
        taxonomyForm.bindFromRequest().fold(
          formWithErrors => BadRequest(views.html.index(formWithErrors)),
          result => {
            Taxonomies.create(result._1, xml)
            Redirect(routes.Application.index())
          }
        )
      }.getOrElse {
        Redirect(routes.Application.index())
      }
  }

and my form is this:

val taxonomyForm = Form(
  tuple(
    "label" -> text,
    "xml" -> text
  )
)

The problem is that bindFromRequest() always fails (causing a bad request to be returned to the client).

Does anybody have an idea where the problem might lie?

Note: I am aware that there is a bug in 2.1.0 that manifests when no files are selected in an upload field; it does not seem to be related, however.

like image 541
Hbf Avatar asked May 22 '26 09:05

Hbf


1 Answers

As far as I know the xml should not be part of the form definition as you get it directly from the request body.

like image 146
EECOLOR Avatar answered May 24 '26 03:05

EECOLOR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!