• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

file download dialog box - help me out

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the code for file download dialog box.

response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment; filename=" + file.getName());
ServletOutputStream sos = response.getOutputStream();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
out1.write(file.getContent());
sos.print(out1.toString());
sos.close();

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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic