PDA

View Full Version : [SOLVED] how to download a server's file dynamically?



pirusira
4 Aug 2010, 2:23 AM
Ext.Ajax.request({
url: 'CsvExport.action',
method: 'POST',
params: {
str: str
},
success: function (response) {
Ext.Ajax.request({
url: 'FileDownload.action',
method: 'POST',
params: {
basePath: 'c:/',
fileName: 'printed_list.csv'
},
success: function (response) {
},
failure: function (response) {}
});
},
failure: function (response) {}
});

CsvExport.action is to create a csv file and FileDownload.action is to download the file.
It's supposed to create a csv file and then download the file.

The following is a setting of FileDownload action.


<action name="FileDownload" class="action.deviceList.FileDownLoadAction">
<result type="stream">
<param name="contentType">binary/octet-stream</param>
<param name="contentLength">${contentLength}</param>
<param name="contentDisposition">${contentDisposition}</param>
<param name="inputName">input</param>
<param name="bufferSize">4096</param>
</result>
</action>


And this is the java code of the action.


public String execute() throws Exception {
String path = basePath + System.getProperty("file.separator") + fileName;
File f = new File(path);
setContentLength(f.length());
setContentDisposition("attachment;filename=" + URLEncoder.encode(fileName, "euc-kr"));
setInput(new FileInputStream(path));
return SUCCESS;
}


The response of the request is ok like this:
21783

But i want to download the file.

Anybody knows?

Thanks in advance.
Stan

Condor
4 Aug 2010, 2:33 AM
You can't use Ext.Ajax.request to download a file.

You could use something like Ext.ux.Report (http://www.sencha.com/forum/showthread.php?80036-SOLVED-Download-data-from-variable-with-a-browser-prompt&p=385087#post385087) when your server sets Content-disposition:attachment.

pirusira
4 Aug 2010, 2:46 AM
Then do I have to remove this ajax request?


Ext.Ajax.request({
url: 'FileDownload.action',
method: 'POST',
params: {
basePath: 'c:/',
fileName: 'printed_list.csv'
},
success: function (response) {},
failure: function (response) {}
});


and..



Ext.getCmp('report').load({
url: './server.php',
params: {
task: 'csv',
query: reportStore.getReportInfo('name', report_id)
}
});

What are the url, task&query properties in the code above?

Animal
4 Aug 2010, 2:55 AM
How come this keeps coming up over and over again?

You download by clicking on a link. So give them a link to click.

Condor
4 Aug 2010, 2:58 AM
url and params are identical to your Ext.Ajax.request.

Differences:
1. You can't specify the method. Ext.ux.Report always uses 'GET'.
2. There is no support for success or failure handlers (browsers don't fire an iframe load event for attachments).

And as Animal mentioned:
This is not the only method. You can also use an <a> tag or post a hidden form to target _blank.

pirusira
5 Aug 2010, 6:40 PM
I solved using the 'report'.
Thanks Condor and Animal!