Fernando André
Fernando André

Reputation: 1213

Upload Form in playframework error 413 request entity to large

This here his the Controller Action also, can I make this async(just curious)?

def upload = Action(parse.multipartFormData) { request => 
    request.body.file("picture").map { picture =>
        import java.io.File
        val filename = picture.filename 
        val contentType = picture.contentType
        picture.ref.moveTo(new File("/tmp/picture"))
        Ok("File uploaded")
      }.getOrElse {
        Redirect(routes.Application.index).flashing(
          "error" -> "Missing file"
        )
      }
  }

Route

GET     /admin/carro/upload         controllers.AdminCarro.upload

Template

@import helper._


@main(new Main("Car Dealers", "Compra e venda de carros", "logo.png", "carro, compra, venda")) {
    <div class="container">
    @form(routes.AdminCarro.upload, 'enctype -> "multipart/form-data") {

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

        <p>
            <input type="submit">
        </p>

    }
    </div>
}

This 413 Request entity to large happens without sending any file or having anything presented.

Upvotes: 0

Views: 624

Answers (1)

jakob
jakob

Reputation: 6005

I'm not sure what you are trying to achieve by doing it like this. Why don't you present the view with a GET and the upload with a POST:

def uploadView = Action.async { request => 
   Future.successful(Ok(views.html.uploadForm))
}

def upload = Action(parse.multipartFormData).async { request => 
    request.body.file("picture").map { picture =>
        import java.io.File
        val filename = picture.filename 
        val contentType = picture.contentType
        picture.ref.moveTo(new File("/tmp/picture"))
        Future.successful(Ok("File uploaded"))
      }.getOrElse {
        Future.successful(Redirect(routes.Application.index).flashing(
          "error" -> "Missing file"
        ))
      }
  }

And the routes file:

  GET     /admin/carro/upload         controllers.AdminCarro.uploadView
  POST    /admin/carro/upload         controllers.AdminCarro.upload

Also if you are uploading large files you could specify the max sixe for all parsers:

parsers.text.maxLength=5242880

or for Action

Action(parse.text(maxLength = 1024 * 10))

Upvotes: 1

Related Questions