Search code examples
javascriptajaxextjsextjs4

ExtJS submit form to download file


Having a really hard time figuring this out. I need to submit a form in an ExtJS application and then download the data in a .CSV file. The problem is, the way ExtJS has me submitting the form with "isUpload" the parameters I'm POSTing are being sent as "mulitpart/form-data" and I can't consume them or parse them. I have multiple values of the same input field name.

field: A
field: B
field: C

When I submit for my grids they go over as multiple instances like above. As soon as I introduce "isUpload" to the form they go overs as:

field: A,B,C

My program reads field as "A,B,C" and not three separate instances of field!

Here's my code. Interesting that when I examine in Firebug the Params tab looks correct, but the POST tab has then all in one value.

I just recently added the parameters to the url to try and fake it out!

    Ext.Ajax.request({
        url : '/cgi-bin/cgijson007.pgm' + '?' + parameters,
        form : myForm,
        params : parameters,
        standardSubmit : true,
        isUpload : true
    });

Solution

  • isUpload: true only defines that you want to upload a file along with fields, so multipart is correct. To download I recommend you to use a hidden frame. For that use a helper defined within a Namespace.

    helper.util.HiddenForm = function(url,fields){
        if (!Ext.isArray(fields))
            return;
        var body = Ext.getBody(),
            frame = body.createChild({
                tag:'iframe',
                cls:'x-hidden',
                id:'hiddenform-iframe',
                name:'iframe'
            }),
            form = body.createChild({
                tag:'form',
                cls:'x-hidden',
                id:'hiddenform-form',
                action: url,
                target:'iframe'
            });
    
        Ext.each(fields, function(el,i){
            if (!Ext.isArray(el))
                return false;
            form.createChild({
                tag:'input',
                type:'text',
                cls:'x-hidden',
                id: 'hiddenform-' + el[0],
                name: el[0],
                value: el[1]
            });
        });
    
        form.dom.submit();
    
        return frame;
    }
    
    // Use it like
    helper.util.HiddenForm('my/realtive/path', [["fieldname","fieldValue"]]);
    

    If the server answer with a download the save window will popup.