我正在使用 play 框架来构建一个网站。我还使用了一个名为xheditor http://code.google.com/p/xheditor/.
Xheditor支持ajax文件上传,它需要服务器端有一个接受包含上传文件的“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 中工作正常,文件数据不为空并且包含正确的数据。但是,如果我使用 chrome 或 firefox,文件数据是null!!
我使用firebug来监控firebug提交的内容,发现它提交了这样的标题:
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 中使用,BUT,仅当上传文件非常小时。例如。小于4K。如果它大一点,例如12K,“IOUtils.copy”方法会报“Read Error!”,甚至下面的代码也会报这样的错误:
request.body.available()
request.body.read()
request.body.read(bytes)