Extjs 4 downloading a file through ajax call

前端 未结 7 1303
陌清茗
陌清茗 2021-01-06 16:16

The problem is very simple: i have to download a file when i submit a form, it\'s an ajax call when the form is submitted which lets me build a file with the data taken from

7条回答
  •  温柔的废话
    2021-01-06 16:51

    After reading on various sources from Ext JS forum and here in stackoverflow, below is the approach I've chosen (using Ext JS version 4.2.1):

    downloadFile: function(config){
        config = config || {};
        var url = config.url,
            method = config.method || 'POST',// Either GET or POST. Default is POST.
            params = config.params || {};
    
        // Create form panel. It contains a basic form that we need for the file download.
        var form = Ext.create('Ext.form.Panel', {
            standardSubmit: true,
            url: url,
            method: method
        });
    
        // Call the submit to begin the file download.
        form.submit({
            target: '_blank', // Avoids leaving the page. 
            params: params
        });
    
        // Clean-up the form after 100 milliseconds.
        // Once the submit is called, the browser does not care anymore with the form object.
        Ext.defer(function(){
            form.close();
        }, 100);
    
    }
    

提交回复
热议问题