Play Framework : Uploading a file to server

In order to upload a file to server we used to use the form with multipart/form-data encoding. File upload is very easy with Play. Use a multipart/form-data encoded request to post files to the server, and then use the java.io.File type to retrieve the file object.

Let us see how we can handle the file upload within Play framework.

Writing an HTML form:

Notice that we’ve given the name “fileUpload” to our upload control.We’ll use same name in our Controller Action. We’ve created the form action as well which is uploadFile in our “Application” controller.

Now define the uploadFile action using a multipartFormData body parser.

Define the POST request in routes in order to call uploadFile action.

GET call will render the view of our application and the POST /upload will upload our file by calling the action defined above.

The Application controller.

Find the complete code here on Github

4 thoughts on “Play Framework : Uploading a file to server

  1. without refreshing page how to upload file , this code is refreshing all page so please suggest me code for without refreshing page.

  2. Hi, Could you please let me know that how can I do that above upload file in Play using javascript or jQuery ? It’s grateful if any code posts here, Thanks in advance.

Leave a Reply

%d bloggers like this: