Multiple files to download using PlayFramework

I try to upload several files at once using the Play Framework, but I always get the first image for each uploaded. Here's a specific case:

HTML:

<form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="file" name="image" /> <input type="file" name="image" /> <input type="file" name="image" /> <input type="submit" name="submit" value="Send images" /> </form> 

Controller:

 public static void upload() { File[] images = params.get("image", File[].class); for (File f : images) { Logger.info (f.getName()); } } 

If I download image1.jpg, image2.jpg, image3.jpg and image4.jpg, Logger.info the console will display:

 image1.jpg image1.jpg image1.jpg image1.jpg 

Other images will not be used.

I tried using List<File> instead of File[] , but it does not work.

I also saw that there is the same question about SO ( here ) that use this as an answer:

 List<Upload> files = (List<Upload>) request.args.get("__UPLOADS"); 

But this does not work in v1.2.4 game !.

I am using Play v1.2.4.

Thank you very much for your help!

+4
source share
3 answers

Ok, I opened a ticket on Play! Framework because this seems like a problem, and apparently I'm not the only one who has this behavior.

I tested the new 1.2.5 and the problem is fixed, at least with the solution I gave to the question:

 public static void upload() { File[] images = params.get("image", File[].class); for (File f : images) { Logger.info (f.getName()); } } 

Note. I am using Java 7!

+2
source

Use automatic binding instead of searching in parameters:

 public class Application extends Controller { public static void index() { render(); } public static void upload(File[] files) { for (File file : files) { Logger.info(file.getName()); } index(); } } 

View Template:

 #{extends 'main.html' /} #{set title:'Home' /} #{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="Send it..." /> #{/} 
0
source

repeated download of the file with the game?

 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 a false File argument, the method will not process multipart / form-data, and you will get an empty request.args array. If someone knows the standard annotation for the game, let me know :)

-1
source

Source: https://habr.com/ru/post/1391258/