views:

2254

answers:

5

Hello Friends,

I want to give file download using java,struts2 and ajax.

On my html page there is a button called "export" clicking on which ajax call will be made which will execute a query and will create .xls file using code and I want to give that file for download to user without storing it on hard drive.

Does any one know how to do that using struts2 and ajax in java?

Is there any example available?

Let me know if you need more details from me...

Thanks.

amar4kintu

+1  A: 

You don't have to use AJAX in this case. Just have your button submit the form to your Struts action, and have the action use the stream result type.

Example:

In your Struts XML:

<result name="download" type="stream">
    <param name="contentDisposition">attachment;filename=report.xls</param>
    <param name="contentType">application/vnd.ms-excel</param>
    <param name="inputName">inputStream</param>
    <param name="bufferSize">1024</param>
</result>

Your action will then provide a public InputStream getInputStream() to pass along the data.

I presume that whatever library you're using to generate the Excel files (POI?) can write the output to an arbitrary OutputStream.

A quick-and-dirty way to convert that to an InputStream:

// Using Workbook from Apache POI for example...
Workbook wb;
// ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
InputStream bis = new ByteArrayInputStream(bos.toByteArray());
ZoogieZork
Hello,I put follwing in my struts.xml<action name="ExportReport" method="ExportReport" class="com.frontend.Interrogations"> <result name="success" type="stream" > <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">fileStream</param> <param name="contentDisposition">attachment;filename=report.xls</param> <param name="bufferSize">1024</param> </result> </action>Now I followed your instructions but it gives file for download with action name that is ExportReport.action instead of report.xlsSo what should be the reason?
amar4kintu
So, you was using MSIE? Either pass filename along as URL pathinfo, or use a better webbrowser.
BalusC
I am using firefox and it should give me download file as report.xls but instead it gives me ExportReport.action to download. and If I rename it to report.xls. It shows perfect .xls file that I am exporting..Can anyone tell what am I missing?
amar4kintu
A: 

atlast, I was able to solve it as following.. I wrote following line in my action class function and I was able to download file with name report.xls instead of ExportReport.action. I do not know exactly .. why?

response.setHeader("Content-Disposition","attachment;filename=rpt.xls");

Following is in my struts.xml file. I removed param from it because it was not working from struts.xml file and I put it in my action Java file as above.

fileStream application/vnd.ms-excel 1024

Hope this will help someone.

Thanks.

amar4kintu

amar4kintu
A: 

I'd use this kind of annotation on the Action class:

@Result(name = "success", type= StreamResult.class,
          params = {"contentType", "application/vnd.ms-excel",
                    "contentDisposition", "attachment; filename=report.xls"},
          value = "reportFileStream"
)
Aleksey Otrubennikov
A: 

@ZoogieZork
Your answer gave me exactly what I needed. Thanks so much!
I'd vote your answer up if I had the reputation needed to do so.

CalicoBeard
A: 

But how do i actually have a dynamic filename, if using annotations

Willie