Đin*_*hâu 5 extjs file response
我使用ExtJS为我的程序构建客户端.有一种情况我想向服务器发送Ajax请求,并获取响应文件(二进制文件,而不是纯文本文件,即XLS或PDF).如何通过ExtJS获取返回的文件(我的意思是该文件可以下载并存储到客户端)?我不能var result = Ext.decode(response.responseText)用来接收结果,因为响应包含二进制数据并且无法解码.
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!')
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的服务器操作返回文件:
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();
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
如果您需要使用典型浏览器提供的打开/保存对话框来提示用户,则不需要进行此调用 AJAX。
只需从您的页面链接到myController/exportFile就足够了。
例如<a href="myController/exportFile">my file</a>
为了使这种方法发挥作用,HTTP 响应myController/exportFile必须包含适当的标头(即 Content-type 和 Content-disposition),告诉浏览器“这是文件。显示打开/保存对话框”,并且根据您的代码片段,我看到您已经解决了这个问题。
| 归档时间: |
|
| 查看次数: |
8209 次 |
| 最近记录: |