我有这个问题。 在合成中,我有一个下载按钮,单击该按钮时,会执行如下 Ajax 请求:
Ext.Ajax.request({
url: 'DownloadServlet',
method: 'GET',
});
此请求由读取默认文件的 servlet 处理。 文件读取成功,其实在firebug中,GET的状态是“200”。
问题是前端(extjs)似乎无法下载文件。我希望浏览器显示经典的更多窗口来下载文件。
它会是什么?谢谢
最佳答案
试试这个
downloadWhatever : function() {
Ext.core.DomHelper.append(document.body, {
tag : 'iframe',
id : 'downloadIframe',
frameBorder : 0,
width : 0,
height : 0,
css : 'display:none;visibility:hidden;height:0px;',
src : '/your/restful/url/'
});
},
服务器端 Spring (如果你想知道的话)
@RequestMapping(value = "/url", method = RequestMethod.GET)
public void download(HttpServletRequest request,
HttpServletResponse response) throws ServletRequestBindingException, IOException {
String tempFile = .. get your file
File file = new File(tempFile);
// in case you have zipped it ...
// else set the correct ContentType
response.setContentType("application/zip");
response.setContentLength((int) file.length());
response.setHeader("fileName", file.getName());
response.setHeader("Content-Disposition", "attachment; filename="+ file.getName());
InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
}
关于javascript - 使用 Ajax 请求下载文件 Extjs4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13050311/