Download a file using Ext JS 6

I am trying to download a file from the server with the Ext JS 6 component based on this message: download the file via Ext js 4

This is the component code:

Ext.define('Aft.view.search.FileDownload', {
extend: 'Ext.Component',
alias: 'widget.acw-fileDownload',
autoEl: {
    tag: 'iframe',
    cls: 'x-hidden',
    src: Ext.SSL_SECURE_URL
},
load: function(config){
    var e = this.getEl();
    e.dom.src = config.url +
        (config.params ? '?' + Ext.urlEncode(config.params) : '');
    e.dom.onload = function() {
        if(e.dom.contentDocument.body.childNodes[0].wholeText == '404') {
            console.
            Ext.create('Acw.view.commons.notifications.Error', {
                html: 'The document you are after can not be found on the server.',
                closeAll: true
            }).show();
        }
    }
}
});

This is the call to get the file:

doExportData: function(){
    this.getView().load({
        url: '/aft/faults/download-file'

});

And this is the Spring service, returning the mock file:

@RequestMapping(value = "/download-file", method = RequestMethod.POST)
public void downloadFilePost(HttpServletResponse response) {

    String csvFileName = "searchFaults.csv";
    response.setContentType("text/csv");

    // creates mock data
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", csvFileName);
    response.setHeader(headerKey, headerValue);

    try {
        CSVWriter writer = new CSVWriter(response.getWriter(), '\t');
        String[] entries = "first#second#third".split("#");
        writer.writeNext(entries);
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

However, when I make a call, I get something like this:

[E] Ext.JSON.decode (): you are trying to decode an invalid JSON string: "first", "second", "third", Search error: you are trying to decode an invalid JSON string: "first" "second" "third"

, - , JSON. , RequestMethod GET, URL- , , , .

- , / ?

+4
1

:

doExportData: function(config){
    window.location.assign(config.url + 
   (config.params ? '?' + Ext.urlEncode(config.params) : ''));
});
+4

Source: https://habr.com/ru/post/1618463/


All Articles