开发者

multi file upload with play?

开发者 https://www.devze.com 2023-04-04 07:39 出处:网络
I try to upload multipl开发者_开发百科e files with one request. My code looks like the following:

I try to upload multipl开发者_开发百科e files with one request. My code looks like the following:

<form action="/application/overviewsubmit" method="POST" enctype="multipart/form-data">
 <input type="file" name="files">
 <input type="file" name="files">
 <input type="submit" value="Run...">
</form>

And the controller:

public static void overviewSubmit(List<File> files){
 System.out.println(files);
}

If both files are set by the user it works. But if the user chooses only one of them and leaves the other one untouched, files is always null.


I've found a hackish way.

You have to import play.data.Upload or play.data.*

public static void overviewsubmit(File fake) {
    List<Upload> files = (List<Upload>) request.args.get("__UPLOADS");
    for(Upload file: files) {
        Logger.info("Size = %d", file.getSize());
    }
}

Without the File fake argument the method will not handle multipart/form-data and you'll get an empty request.args array. If anyone knows the play/standard annotation for it, let me know :)

You can check this for other useful functions - http://www.playframework.org/documentation/api/1.2.3/play/data/FileUpload.html

Hope it'll solve your problem.


I had the same problem but with an input field for multiple itens.

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

The problem was solved using an array instead of a List, in the action parameters:

public static void overviewSubmit(File[] files){
    System.out.println(files);
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号