-
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
-
7 Dec 2011 3:11 AM #7
-
6 Aug 2012 11:16 AM #8
-
16 Aug 2012 12:33 AM #9
a while ago I created a Downloader that handles error messages, maybe you could use it

Code:/** * @class App.util.Downloader * @singleton * * @example * <pre> * App.util.Downloader.get({ * url: 'http://example.com/download?filename=test.txt', * params: { * param1: 'value1' * } * }); * </pre> * */ Ext.define('App.util.Downloader',{ /** * Singleton class * @type {Boolean} */ singleton: true, downloadFrame : null, downloadForm: null, /** * Get/Download from url * @param config */ get: function (config){ var me = this, body = Ext.getBody(); config = config || {}; /** * Support for String config as url */ if(Ext.isString(config)){ config = { url: config }; } me.downloadFrame = body.createChild({ tag: 'iframe', cls: 'x-hidden', id: 'app-upload-frame', name: 'uploadframe' }); me.downloadForm = body.createChild({ tag: 'form', cls: 'x-hidden', id: 'app-upload-form', target: config.target || 'app-upload-frame' }); Ext.Ajax.request({ url: config.url || '.', params: config.params || {}, form: me.downloadForm, isUpload: true, scope: me, success: me.handleException, failure: me.handleException }); }, handleException: function(response,options){ var me = this, result = Ext.decode(response.responseText,true); if(result){ Ext.Msg.alert('Message',result['message']); }else{ Ext.Msg.alert('Message',' An unknown Error occurred while downloading.'); } } });
just output the file to the user if everything is ok, or return
Code:{success: false,message: 'Whatever ......'}
cheers


Reply With Quote
