PDA

View Full Version : Download File From Content



samos
11 Sep 2012, 8:40 AM
Hello,

I need to download a generated file from content.

I get content from ajax and after i want to download a file WITH this content.

Is is possible ?

I already tried to create a form with a hidden field, datas are not sent, same for textarea, because the datas are too big ?

Thanks for help,

Samos

scottmartin
11 Sep 2012, 2:07 PM
I need to download a generated file from content.

Is the file generated on the server and you want to display/save it to client?


I already tried to create a form with a hidden field, datas are not sent, same for textarea, because the datas are too big

I am not sure I understand this ... do you mean sending file to server?

Please detail what you are trying to achieve.

Scott.

samos
11 Sep 2012, 2:40 PM
Hello Scott,

I receive from server the content of a file (via ajax request), i want to create a file and save it on local hard drive with this content.

Samuel

scottmartin
11 Sep 2012, 3:04 PM
This is a security risk and not supported. Imagine some website trying to save a file to your PC.
There are however several approaches online I believe if you search around the net.

If you send the file, you can use window.open(file) to open a save dialog.

Scott.

samos
11 Sep 2012, 3:06 PM
Yes i want to save the file with a "save as" dialog, but i really don't know how to do.

scottmartin
11 Sep 2012, 4:33 PM
This is more of a browser issue ... server has to set the content, browser will open.

Here is an example:
http://examples.extjs.eu/

Go to Form/Downloading Files

Has demo and source.

Scott.

samos
12 Sep 2012, 1:20 AM
It is not a good example because the form request a file (download.php), me i get only content of this file, and from this content generate a file.

samos
12 Sep 2012, 3:03 AM
var results = Ext.decode(response.responseText) ;
window.open("data:text/csv;base64," + window.btoa(results), "_blank");

scottmartin
12 Sep 2012, 4:21 AM
As I mentioned above .. glad you got it working ...

Scott.

somewhereinlondon
27 Sep 2012, 7:49 AM
I'm also trying to download a file (generated on the fly, on the server).

When I try:

var results = Ext.decode(response.responseText) ;
window.open("data:text/csv;base64," + window.btoa(results), "_blank");

as in:

Ext.Ajax.request({
url: app_constants.EXPORT_TO_EXCEL_URL,
method: 'POST', componentName: gridtoexport,

success:
function(result, request) {
var results = Ext.decode(result.responseText) ;
window.open(
"data:text/csv;base64," + window.btoa(results), "_blank");
},

failure:
function(result, request) {

Ext.MessageBox.alert(
'Failed', result.responseText);
},
jsonData: payload
});
};



.. I get (in the browser):
Uncaught Ext.Error: You're trying to decode an invalid JSON String

How can I get ext to understand that this is a binary file and not a json string ? (I've already set the correct content type etc. on the serverside).

Any ideas how I can read the binary file out of response.responseText ?
Oh - and I'm using Ext js 4.1.2