使用不同浏览器在playframework中上传文件

Fre*_*ind 7 firefox internet-explorer google-chrome file-upload playframework

我正在使用playframework来构建一个网站.我还使用了一个名为xheditor的丰富编辑器.

Xheditor支持ajax-fileuploading,它需要服务器端有一个动作接受包含上传文件的"filedata"参数.

所以我写了一个上传动作:

public class Application extends Controller {
    public static void upload(File filedata) { 
        // the filedata should not be null
        renderText("{'err':'', 'msg':{'ur':'/uploaded/xxx.zip'}}"); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

它在IE6中工作正常,filedata不为null并包含正确的数据.但是,如果我使用chrome或firefox,则filedata为null!

我使用firebug监视firebug提交的内容,并发现它提交了这样一个标题:

content-disposition
attachment; name="filedata"; filename="051111twdns.zip"
Run Code Online (Sandbox Code Playgroud)

我认为play没有正确处理这种情况,所以参数"filedata"为null.

为了使用chrome和firefox,我修改了这个动作:

public class Application extends Controller {
    public static void upload(File filedata) { 
        if(filedata!=null) {
            // ok, it's IE6
            renderText("{'err':'', 'msg':{'ur':'/uploaded/xxx.zip'}}"); 
        } else {
            // it's chrome or firefox, the data is in request.body
            File targetFile = new File("upload/test.zip");
            IOUtils.copy(request.body, new FileOutputStream(targetFile));
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

这在IE6,Chrome和Firefox现在的工作,,只有当上传文件是非常小的.例如小于4K.如果它有点大,例如12K,方法"IOUtils.copy"将报告"读取错误!",即使以下代码也会报告此类错误:

request.body.available()
request.body.read()
request.body.read(bytes)
Run Code Online (Sandbox Code Playgroud)

man*_*ian 0

您应该看一下 play.data.parsing.ApacheMultipartParser 类,它管理从 HTTP 请求提取文件附件。

getFieldName 获取搜索头“content-disposition”和“form-data”的字段名称。在你的情况下,它不存在。

private String getFieldName(Map /* String, String */ headers) {
    String fieldName = null;
    String cd = getHeader(headers, CONTENT_DISPOSITION);
    if (cd != null && cd.toLowerCase().startsWith(FORM_DATA)) {

        ParameterParser parser = new ParameterParser();
        parser.setLowerCaseNames(true);
        // Parameter parser can handle null input
        Map params = parser.parse(cd, ';');
        fieldName = (String) params.get("name");
        if (fieldName != null) {
            fieldName = fieldName.trim();
        }
    }
    return fieldName;
}
Run Code Online (Sandbox Code Playgroud)

在 getFileName 中,它搜索标头“content-disposition”,然后搜索“form-data”或“attachment”以获取文件名。

private String getFileName(Map /* String, String */ headers) {
    String fileName = null;
    String cd = getHeader(headers, CONTENT_DISPOSITION);
    if (cd != null) {
        String cdl = cd.toLowerCase();
        if (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) {
            ParameterParser parser = new ParameterParser();
            parser.setLowerCaseNames(true);
            // Parameter parser can handle null input
            Map params = parser.parse(cd, ';');
            if (params.containsKey("filename")) {
                fileName = (String) params.get("filename");
                if (fileName != null) {
                    fileName = fileName.trim();
                    // IE7 returning fullpath name (#300920)
                    if (fileName.indexOf('\\') != -1) {
                        fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
                    }

                } else {
                    // Even if there is no value, the parameter is present,
                    // so we return an empty file name rather than no file
                    // name.
                    fileName = "";
                }
            }
        }
    }
    return fileName;
}
Run Code Online (Sandbox Code Playgroud)

显然,在您的情况下,这段代码将找到文件名,但找不到字段名称,所以也许这就是您在控制器中将文件数据字段设置为 null 的原因。

为什么它可以在 IE6 上运行?(因为它从来都不是真正的标准并且做了其他人不再做的事情???:))

有关信息,在 Crud 模块中,fileField.html 声明文件上传如下:

<input id="${field.id}" class="${field.errorClass}" type="file" name="${field.name}" />
Run Code Online (Sandbox Code Playgroud)

问候