tags:

views:

25

answers:

2

I know that this question is quite common, but its specifics are stumping me.

I have an "export" button which I want to take a load of data generated, create a CSV file, then pop up a Save File As dialog box, to save that file on their local machine.

The name of this file is dynamic, also.

I know how to make the CSV file, but how do I then pop up a box on the client side to let the user download it?

I am using JSP with Struts

+1  A: 

All you need to do is to send a response containing your generated csv with a header:

Content-Disposition: attachment; filename=your_file.csv
Max
I am putting the filename in as a complete path (/home/joeblogs/blah.csv) and it pops up to save the current JSP page I am on??
MichaelMcCabe
No, this header specifies how browser needs to treat the data it receives. So you need to type a name that will appear in `Save As...` dialog. For example `mydata.csv` or something.
Max
A: 

Thanks to Max for his answer, but after getting further with this I am hitting a new problem. I am setting the header of the response, then creating a ServletOutputStream to pass my data to. The problem I am having, is that I am getting an error

getOutputStream() has already been called for this response

I have found that the reason for this, is that my Action class is returning an ActionForward and this will attempt to write more data to the output stream.

Is there a way of getting around this without removing the return type, because this is needed elsewhere.

MichaelMcCabe
Well I figured out that if I just return to the same page, it works
MichaelMcCabe