Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple upload file with PlayFramework

I'm trying to upload multiple files at once with Play Framework, but I always get the first image for each uploaded. Here's a concrete case :

The 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>

The controller :

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

If I upload image1.jpg, image2.jpg, image3.jpg & image4.jpg, the Logger.info on the console will display :

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

The other images won't be used.

I tried with List<File> instead of File[] but it doesn't work neither.

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

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

But it doesn't work in the v1.2.4 of Play!.

I'm using Play v1.2.4.

Thank you really much for your help!

like image 938
Cyril N. Avatar asked Sep 13 '26 13:09

Cyril N.


1 Answers

Well, I have opened a ticket at Play! Framework because it seems to be problem, and apparently, I'm not the only one to have this behavior.

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

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

Note: I'm using Java 7!

like image 77
Cyril N. Avatar answered Sep 18 '26 04:09

Cyril N.