Use ExtJS Answer File

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) {
        // What should I do to get the file?
    },
    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!

+5
source share
2 answers

, / , AJAX.

myController/exportFile .
, <a href="myController/exportFile">my file</a>

HTTP myController/exportFile ( Content-type Content-disposition), " . show open/save dialog" , , .

+2

window.open('http://your.domain/your/file'). http://your.domain/your/file -link

0

All Articles