我正在使用 playframework 来构建一个网站。而且我还使用了一个名为 xheditor 的富编辑器.
Xheditor 支持ajax-fileuploading,它需要服务器端有一个 Action 接受包含上传文件的“filedata”参数。
所以我写了一个上传 Action :
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 中工作正常,文件数据不为空并且包含正确的数据。但是,如果我使用 chrome 或 firefox,文件数据是 空 !!
我使用firebug来监控firebug提交的内容,发现它提交了这样一个header:
content-disposition
attachment; name="filedata"; filename="051111twdns.zip"
我认为 play 没有正确处理这种情况,所以参数“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”会报“Read Error!”,即使是下面的代码也会报这样的错误:
request.body.available()
request.body.read()
request.body.read(bytes)
最佳答案
尝试将您的网站与文件上传器集成,其中包含大量不同语言的文档/示例
www.uploadify.com/
关于internet-explorer - 使用不同浏览器在 Play Framework 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863443/