I use ExtJS to build the client-side for my program. There's a situation that I want to send an Ajax request to server, and get the response file (binary file, not plain text file, i.e XLS or PDF). How can I get that returned file by ExtJS (I mean that file can be downloaded and stored to client)? I cannot use var result = Ext.decode(response.responseText)
to receive the result because reponse contains binary data and it cannot be decoded.
The Ajax call is very simple :
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!')
}
});
Here is my server action to return 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 so much!