【问题标题】:Multiple upload file with PlayFramework使用 PlayFramework 多次上传文件
【发布时间】:2012-01-16 13:21:40
【问题描述】:

我正在尝试使用 Play Framework 一次上传多个文件,但我总是为每个上传的文件获取第一张图片。这是一个具体的案例:

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>

控制器:

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

如果我上传image1.jpg、image2.jpg、image3.jpg和image4.jpg,控制台上的Logger.info会显示:

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

不会使用其他图片。

我尝试使用List&lt;File&gt; 而不是File[],但它也不起作用。

我在 SO (here) 上也看到了同样的问题,用这个作为答案:

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

但在 Play! 的 v1.2.4 中不起作用。

我正在使用 Play v1.2.4。

非常感谢您的帮助!

【问题讨论】:

    标签: file-upload playframework


    【解决方案1】:

    好吧,我在 Play 上开了一个ticket!框架,因为它似乎有问题,而且显然,我不是唯一一个有这种行为的人。

    我用新的 1.2.5 进行了测试,问题得到了解决,至少我在这个问题上给出的解决方案是这样的:

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

    注意:我使用的是 Java 7!

    【讨论】:

    • 1.2.5没试过,直接换2.x。如果有人在这里更新,我很乐意给他/她接受的答案。如果我有时间,我会尝试使用 1.2.5 分支进行测试并更新我的答案。
    • 如果您使用的是 java 6,您能告诉我它是否仍然无法工作或是否已修复?谢谢:)
    【解决方案2】:

    使用自动绑定而不是查看参数:

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

    视图模板:

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

    【讨论】:

    • 它对我不起作用:/(如果我使用 Play!v1.2.4 和 java 1.6.0_26)。我仍然有一个与我上传的文件一样多的文件列表,但名称相同(第一个)。
    【解决方案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());
        }
    }
    

    如果没有 File fake 参数,该方法将无法处理 multipart/form-data 并且您将获得一个空的 request.args 数组。如果有人知道它的播放/标准注释,请告诉我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多