PDA

View Full Version : How to Download a non persistent file?



Malro
18 Jun 2013, 3:42 AM
Hello, i want to download a generated excel document.

for now, i just use the window.location way. e.g.:


window.location = 'myServerSie.php?_dc'+ new Date().getTime() + urlParams;

thats working ok. At serverside i pass the arguments, select from db, style the excel document and return it.


header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '".xls"');
header('Cache-Control: max-age=0');

that easy way has 2 contras.
error handling --> if php fails i return something like

exit("<script>alert('Error bla bla.'); history.back();</script>");
if php needs some time, user will be irritated. (how to use a loadMask?)

I know the "linkbutton" and iframe solution, but how to use a loadMask?
Is it better to use an ajax.request to generate the file and if request is successful to begin the download? (that way, i have to do some cleaning ...)

tvanzoelen
18 Jun 2013, 4:15 AM
Is it better to use an ajax.request to generate the file and if request is successful to begin the download? (that way, i have to do some cleaning ...)

I think that is the best option, create the file with an ajax request and return a ticket. With the ticket you download the file.

Between that part you can use a loadMask because you have a callback. By downloading the file, there are some problems though, because it is difficult to get a callback with an Iframe (or else) when you return pdf or xls.

I download the file with this


Ext.Ajax.request(
{
url: url,
params: params,
form: 'anexistingformid',
isUpload: true,
success: function(response, options) {
//never reases the callback if you serve a pdf or excel
console.log('download complete');
},
failure: function(response, options) {
// //never reases the callback if you serve a pdf or excel
console.log('failure');
}
});

This way Extjs creates iframes for you and serves the file with a browser save or opendialog. But I noticed that the iframes were never cleaned up because the callback was never reached. That callback is put on the load event of the iframe. It will never fire when serving a pdf - I think a little bug there.

So I had to modify upload function in the Ext.data.Connection class a bit so that it reached the callback. But that is still before the actual download.

I think getting a callback after the browser download process is almost impossible. I saw some people tweaking....but I have no suitable solution seen yet.

Malro
20 Jun 2013, 12:02 AM
I decided to use an ajax-request to create the file and to start the download in the successful callback by using "window.location = file". It works well, also error handling is easy (except window.location = ...).

// now i can face the other problem. PHPExcel has poor performance. Creating a xls with 10 cols and 5000 rows. needs a lot of memory and time. My workaround is to use csv file instead. no styling vs. fast.

tvanzoelen
20 Jun 2013, 12:11 AM
I do the same with excel, I also generate csv.