abisofty
abisofty

Reputation: 105

Alternate way for downloading file as CSV using ExtJS 4.1 without using Ajax Request?

I need to download data using servlet as CSV File.I am passing a parameter by clicking on the navigation tree from ExtJS 4.1 to the servlet via Ajax Request.Is there any alternate way to pass the parameter to the servlet as ajax can't force the open/save dialog to download?I have to pass the parameter by just clicking on the child of navigation tree itself.Can anybody help me pls?

I searched for the solutions and found like hidden Iframe.I don't know how to adapt this into mine?

Thanks a lot in advance.

Upvotes: 2

Views: 1170

Answers (1)

sra
sra

Reputation: 23973

Use a helper defined within a Namespace. And don't forget to remove it after you are done.

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.

Upvotes: 1

Related Questions