Download file in playframework with various browsers

I use playframework to create a website. And I also use a rich editor called xheditor .

Xheditor supports ajax-fileupload download, it needs a server side that accepts the "filedata" parameter, which contains the download file.

So, I wrote an action to load:

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

It works fine in IE6, filedata is not null and contains the correct data. But if I use chrome or firefox, filedata is null !!

I use firebug to monitor what Firebug represents, and found that it sends such a header:

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

I think the game does not handle this case correctly, so the "filedata" parameter is null.

To work with chrome and firefox, I changed this action:

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

Now it works in IE6, chrome and firefox, BUT only if the download file is very small. For example. less than 4K. If it is slightly larger, for example, 12K, the method "IOUtils.copy" will report "Read Error!", Even the following code will report such an error:

 request.body.available() request.body.read() request.body.read(bytes) 
+7
firefox google-chrome internet-explorer file-upload playframework
source share
2 answers

You should take a look at the play.data.parsing.ApacheMultipartParser class, which controls the extraction of file attachments from an HTTP request.

getFieldName gets the name of the search header for the "content-disposition" and "form-data" fields. In your case, it is not present.

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

in getFileName, it looks for the header "content-disposition" and then "form-data" or "attachment" to get the file name.

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

So, apparently, in your case, this code will find the file name, but not the field name, so that is probably why you have the filedata field equal to null in your controller.

Why does it work with IE6? (because it was never really standard and did what others no longer do ??? :))

For information in the Crud module, the File.html file announces the file download as follows:

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

welcomes

0
source share

Try integrating your site with a file downloader that has documentation / examples for different languages ​​www.uploadify.com/

+1
source share

All Articles