I use ExtJS to create the client part for my program. There is a situation where I want to send an Ajax request to the server, and get an answer file (binary file, not a plain text file, i.e. XLS or PDF). How can I get this returned file using ExtJS (I mean that the file can be downloaded and saved for the client)? I cannot use var result = Ext.decode(response.responseText)to get the result, because the answer contains binary data and cannot be decoded.
Ajax calling is very simple:
Ext.Ajax.request({
url : 'myController/exportFile',
method : 'GET',
success : function(response, opts) {
},
failure : function(response, opts) {
alert('Export file failed!')
}
});
Here is my server action to return the file:
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();
}
Thank you very much!
source
share