7

我正在使用playframework来构建网站。我还使用了一个名为xheditor的丰富编辑器。使用不同浏览器在playframework中上传文件

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'}}"); 
    } 
} 

它在IE6中正常工作,该FILEDATA不为空,并且包含正确的数据。但是,如果我使用chrome或firefox,那么这个题目是null !!

我使用Firebug监控萤火虫提交什么,并发现它提出这样一个标题:

content-disposition 
attachment; name="filedata"; filename="051111twdns.zip" 

我觉得该剧没有正确处理这种情况下,这样的参数“FILEDATA”为空。

为了与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)); 
     } 
    } 
} 

这在IE6,Chrome和Firefox的工作现在,,只有当上传文件是非常小的。例如。小于4K。如果它稍大一些,例如12K,方法“IOUtils.copy”将报告“读取错误!”,就连下面的代码将报告此类错误:

request.body.available() 
request.body.read() 
request.body.read(bytes) 

回答

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

的getFileName,它搜索标题“内容处置”,然后在“表单数据”或“附件”来获取文件名。

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

因此很明显,你的情况,该代码会发现文件名,但不是字段名称,所以也许这就是为什么你有你的FILEDATA字段设置为空在控制器中。

它为什么与IE6一起使用? (因为它从来没有真正的标准,做了别人不办了??? :))

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

<input id="${field.id}" class="${field.errorClass}" type="file" name="${field.name}" /> 

关于

1

尝试用文件上传,其中有文件的LOF /样品整合您的网站diffrent languages www.uploadify.com/