• Post Reply Bookmark Topic Watch Topic
  • New Topic

file download dialog box - help me out  RSS feed

Priyadharshini Nagarajan
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code for file download dialog box.

response.setHeader("Content-Disposition","attachment; filename=" + file.getName());
ServletOutputStream sos = response.getOutputStream();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();

i have a class which gets the content and file name.

for me the save button in the file download dialog box is working.But the open button is not working ie when i open the file it is saying

"Create a new File because it does not exist"

Thanks in advance
Henrique Sousa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Priyadharshini,

I've been facing that problem for over a year now. It has a variant: should you change the content type to "application/stream", you would have to click the Open button twice (IE only, Firefox works fine anyways) . Gladly, I thought of a simple solution a few days ago.

The problem is that IE sniffs the response instead of just interpreting it, so the line that causes bugs to pop out is

To avoid that, IE must think that it requested the file with the name you want. Thus, we need a mapping and a servlet to deal with that mapping. Suppose we call that servlet FileServerServlet. So you add to your web.xml:

All right, we can fool the browser at the file name. Now, for the servlet implementation. I have used an approach that may not be the most appropriate, but anyway... replace the code that you posted (save it for later) by this:

Another approach would be to send the file path as a request parameter, but I don't like that (it would be displayed in the browser). At last, the FileServerServlet looks like this:

That's it. This solved my problem, and I hope it does the same for you. Best regards
[ September 28, 2004: Message edited by: Henrique Sousa ]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!