PDA

View Full Version : Extjs 4 downloading a file through ajax call



softwareplay
10 Dec 2013, 8:44 AM
The problem is very simple: i have to download a file when i submit a form, it's an ajax call when the form is submitted which lets me build a file with the data taken from the form, server side, and then send it as a link to an alert. The fact is that my boss want the file to be downloaded directly and not through a link in an alert. So i had to make sure that the file is available server side through tornado(web):



self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=clients_counter.zip')
with open("static/clients_counter.zip", 'r') as f:
while True:
data = f.read()
if not data:
break
self.write(data)
self.finish()

The server side code seems to work fine, but the client side (extjs4.1) is really a nightmare. This is how my ajax call looks like now, and it doesn't work:



Ext.Ajax.request({
method : "GET", url : 'http://whatever.com/count?client='+client+'&start='+start+'&end='+end,
timeout : 30000,
success :
function (response) {
//Ext.Msg.alert(response.responseText);
desktop.getWindow('count-win').doClose();
return response;
}//handler,
failure :
function(response) {
alert("Wrong request");
}});

nicholasnet
10 Dec 2013, 11:00 AM
You cannot download a file via Ajax call. You have to use native form submit with iframe. For more information look into this link http://stackoverflow.com/questions/11782856/extjs-4-downloading-a-file-returned-from-a-post-request

Kevin Jackson
12 Dec 2013, 10:32 AM
Please advise if the information above has been helpful