This is the document of how to uploading a file in play2: https://github.com/playframework/Play20/wiki/ScalaFileUpload
But I have another question: Can’t we use the play1’s way anymore?
In play1, I can upload a file as following:
routes
--------
post /upload Application.upload
Application.java
----------------
public static void upload(File file) { ... }
The action upload will get the uploaded file automatically.
But how to do the same in play2?
I tried:
routes:
--------
POST /upload controllers.Application.upload
Application.scala
-----------------
def upload(file: File) = Action { ...}
But it can’t compiled, the error message is:
not enough arguments for method upload: (file: java.io.File)
Then I add a parameter to routes:
POST /upload controllers.Application.upload(file: java.io.File)
It still reports error:
No QueryString binder found for type java.io.File. Try to implement an implicit QueryStringBindable for this type.
Files are passed in HTTP requests body, but in Play 2 the parameters of an action can only be bound to the URL path or the query string [1]. You need to retrieve uploaded files from the request body, in your action body, as explained in the documentation [2].
[1] https://github.com/playframework/Play20/wiki/ScalaRouting
[2] https://github.com/playframework/Play20/wiki/ScalaFileUpload