Multiple upload file with PlayFramework

一个人想着一个人 提交于 2020-01-02 05:46:07

问题


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!


回答1:


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!




回答2:


Use the automatic binding instead of looking in the params:

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

The 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..." />
#{/}



回答3:


multi file upload with play?

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 :)



来源:https://stackoverflow.com/questions/8880607/multiple-upload-file-with-playframework

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