Michael Haston
Michael Haston

Reputation: 43

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
    });

Upvotes: 3

Views: 9878

Answers (3)

Théo Porto
Théo Porto

Reputation: 1

This works perfectly. Arrays included.

downloadFile: function (url, params) {
        debugger;
        var body = Ext.getBody(),
            frame = body.createChild({
                tag: 'iframe',
                cls: 'x-hidden',
                id: 'hiddenform-iframe',
                name: 'iframe'
            }),

            form = body.createChild({
                tag: 'form',
                method: 'POST',
                cls: 'x-hidden',
                id: 'hiddenform-form',
                action: url,
                target: 'iframe'
            });

        for (var i in params) {

            if (Ext.isArray(params[i])) {
                for (var j = 0; j < params[i].length; j++) {
                    form.createChild({
                        tag: 'input',
                        type: 'hidden',
                        cls: 'x-hidden',
                        id: 'hiddenform-' + i,
                        name: i,
                        value: params[i][j]
                    });
                }
            } else {

                form.createChild({
                    tag: 'input',
                    type: 'hidden',
                    cls: 'x-hidden',
                    id: 'hiddenform-' + i,
                    name: i,
                    value: params[i]
                });
            }

        }

        form.dom.submit();

        return frame;
    }

Upvotes: 0

Leng Weh Seng
Leng Weh Seng

Reputation: 755

Below is how I handler a post download on a grid listing:

First, I create html form container with hidden filed contains the parameters to post, and also a submit function.

var txtFileId = Ext.create('Ext.form.field.Hidden', { name: 'fid', value: 0 });
var dwFrm = Ext.create('Ext.container.Container', {
  autoEl: { tag: 'form',  method: 'POST', target: '_BLANK', 
    action: '/download/files' }, style: { hidden: true },
  items: [txtFileId, {
    xtype: 'hidden', name: 'userId', value: '1111'
  }],
  submit: function (fid) {
    if (fid) {
      txtFileId.setValue(fid);
      this.el.dom.submit();
    }
  }
});

Second, I dock the above component into grid's toolbar

var grid = Ext.create('Ext.grid.Panel', {
  .....
  dockedItems: [Ext.create("Ext.Toolbar", {
    dock: "top", items: [
      dwFrm, 
      { text: "Download Selected",
        handler: function () {
          var sm = grid.getSelectionModel();
          if (!sm.hasSelection()) return null;
          var recs = sm.getSelection();
          dwFrm.submit(recs.data.id);
        }
      }
    ]
  })]
  .....
});

Upvotes: 0

sra
sra

Reputation: 23973

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.

Upvotes: 2

Related Questions