harshvardhan
harshvardhan

Reputation: 805

Multipart file upload in Scala Play

I have been trying to upload a file on my Scala Play server. I have followed the tutorial given on Play framework documentation page dealing with file uploads. Following the instructions provided there, I have first created a HTML page called fileuploadform.scala.html in the views folder. The file hosts the following code

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

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

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

}

Then, I have created two actions in controller. One will honor the GET request to load the fileuploadform html and the other will honor the POST request upon click of the Upload button on form.

The two actions in the controller (ScalaFileUploadController.scala) are:

def uploadForm = Action {
    Ok(views.html.fileuploadform())
  }

and

  def upload = Action(parse.multipartFormData) { request =>
    request.body.file("picture").map { picture =>
      val filename = Paths.get(picture.filename).getFileName
      picture.ref.moveTo(Paths.get(s"/path/to/location/$filename"), replace = true)
      Ok("File uploaded")
    }.getOrElse {
      Redirect(routes.ScalaFileUploadController.index).flashing(
        "error" -> "Missing file")
    }
  }

Finally, in the routes, I have defined the routing as

GET  /uploadForm                    controllers.ScalaFileUploadController.uploadForm
POST /upload                        controllers.ScalaFileUploadController.upload()

The application is running on port xxxx

When I hit the url ip.ip.ip.ip:xxxx/uploadForm, I get a COMPILATION ERROR

not found: value Paths

And the line val filename = Paths.get(picture.filename).getFileName gets highlighted.

Am I missing some library that is to be added or some syntax modification?

Upvotes: 0

Views: 1206

Answers (1)

o-0
o-0

Reputation: 1789

You need to import the following:

import java.nio.file.Paths

Upvotes: 1

Related Questions