Hybrid View

  1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    3
    Vote Rating
    0
    peterson.ron is on a distinguished road

      0  

    Default How to download a file using Ext.Ajax using a POST call?

    How to download a file using Ext.Ajax using a POST call?


    I'm a newby in Ext JS and I need to make a restful call that returns a file to the browser. How should I do this?
    This is what I have so far, the call is return but I can't get the file downloaded to the browser.



    Code:
    Ext.onReady(function () {
    
    form = Ext.Ajax.request({
        url: 'http://localhost:8080/getfile',
        success: function(x){
                     return x; 
                     },
        failure: function () { console.log('failure');},
        params: {'someparams': Ext.encode ({'abc': {'hello': '123'}})
                }
        });
    });
    I could get it to work with a GET call using:

    window.open('http://localhost:8080/getfile', 'Download')

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,120
    Vote Rating
    509
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    You can't. You either need to embed a hidden iframe or use window.open like you've suggested.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  3. #3
    Sencha User
    Join Date
    Nov 2011
    Posts
    3
    Vote Rating
    0
    peterson.ron is on a distinguished road

      0  

    Default


    I need to make a POST call so window.open will not work for me.

    Would you please send me a sample how to call it with hidden iFrame?

  4. #4
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      1  

    Default


    the legal way is to post the request to a script (eg php) and this script sends filecontent with file header.
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  5. #5
    Sencha User
    Join Date
    Nov 2011
    Posts
    3
    Vote Rating
    0
    peterson.ron is on a distinguished road

      0  

    Default


    Yes, I am using this convention. I have a service returning headers "contnet-disposition as attachment and content-type as text".
    And when I try directly from a URL I'm getting the correct pop-up dialog from the browser.

    My QUESTION is how to do it from Ajax. How to make a POST call and have the browser pop-up the dialog "File Download" from the response?

  6. #6
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    use an iframe like this:

    PHP Code:
    Ext.Ajax.request ({
      
    url     'askfordownload',
      
    timeout 30 60 1000// 30 Minutes should be OK.
      
    scope   this,
      
    params  Ext.applyIf(paramsApp.config.baseParams),
      
    success : function (r) {
        var 
    resp    Ext.decode(r.responseText);
        
    Ext.DomHelper.append(Ext.getBody(), {
          
    tag:          'iframe',
          
    frameBorder:  0,
          
    width:        0,
          
    height:       0,
          
    css:          'display:none;visibility:hidden;height:0px;',
          
    src:          resp.downloadUrl
        
    });
      },
      
    failure: function(r) {
        
    alert('error');
      }
    }); 
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar