Hybrid View
-
2 Nov 2011 3:25 PM #1
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.
I could get it to work with a GET call using: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'}}) } }); });
window.open('http://localhost:8080/getfile', 'Download')
-
2 Nov 2011 4:29 PM #2
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 Nov 2011 10:49 AM #3
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?
-
3 Nov 2011 3:52 PM #4
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
energlobe.de - german online magazine
-
4 Nov 2011 9:28 AM #5
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?
-
5 Nov 2011 5:44 PM #6
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(params, App.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
energlobe.de - german online magazine


Reply With Quote