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:

@helper.form(action = routes.Application.uploadFile, 'enctype -> "multipart/form-data") {
 <input type="file" name="fileUpload">
 <p>
 <input type="submit" value="Upload">
 </p>
 }

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.

def uploadFile = Action(parse.multipartFormData) { request =>
 request.body.file("fileUpload").map { video =>
 val videoFilename = video.filename
 val contentType = video.contentType.get
 video.ref.moveTo(new File("/home/neelkanth/Desktop/" + video.filename))
 }.getOrElse {
 Redirect(routes.Application.index)
 }
 Ok("File has been uploaded")
 }

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

GET / controllers.Application.index
POST /upload controllers.Application.uploadFile

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.

import play.api._
import play.api.mvc._
import java.io.File

object Application extends Controller {

def index = Action {
 Ok(views.html.index("File Upload In Play"))
 }

/**
 * uploadFile action definition
 */
 def uploadFile = Action(parse.multipartFormData) { request =>
 request.body.file("fileUpload").map { video =>
 val videoFilename = video.filename
 val contentType = video.contentType.get
 video.ref.moveTo(new File("/home/neelkanth/Desktop/" + video.filename))
 }.getOrElse {
 Redirect(routes.Application.index)
 }
 Ok("File has been uploaded")
 }
}

Find the complete code here on Github

About Neelkanth Sachdeva

Software Consultant @ Knoldus Software LLP Co-Founder @ My Cell Was Stolen
This entry was posted in News, Scala, Web and tagged , , . Bookmark the permalink.

3 Responses to Play Framework : Uploading a file to server

  1. Even in such small tutorials I suggest taking more care about security – do not use a name of the file in your target path (Application:18: ‘”/home/neelkanth/Desktop/” + video.filename’).

  2. simmant says:

    Thank you so much for a such a nice blog on play but can you please help me to in fileuploading in java on playframework.

  3. Ramdas says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s