multi file upload with play?

强颜欢笑 提交于 2019-11-29 22:42:21

问题


I try to upload multiple 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.


回答1:


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.




回答2:


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);
} 


来源:https://stackoverflow.com/questions/7401364/multi-file-upload-with-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!