Reputation: 13
I try to upload multiple files with one request but it always error, My view code:
<input type="file" name="files[]" multiple>
And:
public static void doUpload(File[] files) {
File dir = new File(Play.applicationPath+File.separator+"public"+File.separator+"uploads");
if (!dir.exists()) {
dir.mkdirs();
}
boolean success = files.renameTo(new File(dir, files.getName()));
if (!success) {
renderText("{'success':'true', 'msg':{'url':'"+files.getName()+"'}}");
} else {
renderText("{'success':'true'}");
}
}
If I use single file upload with <input type="file" name="files">
and parameters in controllor using File files
, then it works fine.
thank you.
Upvotes: 0
Views: 1489
Reputation: 6486
This works fine in Play 1.2.5 with the multiple tag in <input type="file" name="files" multiple>
. The only problem I see with the original posters code was name="files[]"
which should have been name="files"
It may have been the case that there was also a bug in an earlier version of Play, but this just worked for me (I selected 50 files with one file input)
Upvotes: 0
Reputation: 8202
If you have multiple inputs with the same name, the controller will receive those files with the given name as an array, e.g.
Controller:
public static void upload(File[] files) {
...
}
View:
#{form @upload(), enctype:'multipart/form-data'}
<input type="file" name="files">
<input type="file" name="files">
<input type="file" name="files">
<input type="submit" value="Upload" />
#{/form}
You can always add a bit of javascript to allow the user to add additional inputs on the client side
Upvotes: 1