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/