我使用ExtJS为我的程序构建客户端。有一种情况是我想向服务器发送Ajax请求,并获取响应文件(二进制文件,而不是纯文本文件,即XLS或PDF)。我怎样才能通过ExtJS获得返回的文件(我的意思是这个文件可以下载并存储到客户端)?我无法使用var result = Ext.decode(response.responseText)
来接收结果,因为响应包含二进制数据并且无法解码。获取响应文件的使用ExtJS
AJAX调用非常简单:
Ext.Ajax.request({
url : 'myController/exportFile',
method : 'GET',
success : function(response, opts) {
// What should I do to get the file?
},
failure : function(response, opts) {
alert('Export file failed!')
}
});
这里是我的服务器行动回报文件:
public void sendFile(HttpServletResponse response, String filePath) {
def file = new File(filePath);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}");
response.outputStream << file.newInputStream();
}
太谢谢你了!
很好的答案!你是最棒的 !!我已经找了很久,只有这个帖子给了我适当的答案! – davs 2011-07-28 12:47:57
@amol你能帮我吗[这里](http://stackoverflow.com/questions/8505995/itextsharp-generated-pdf-how-to-prompt-a-user-to-choose-where-to-save) – Armance 2011-12-15 09:53:15