Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File Download Servlet Problem  RSS feed

 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand there have been other messages regarding this topic, but I am having as problem I can not find the answer to, so please assist.
I am doing a simple binary download with the following code, and I am getting the wrong path for the file to be downloaded in the dialog that pops up in the browser.
exe_filename = "file_to_download.exe"
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename="+ exe_filename);
This code cause the dialog to pop up stating the wrong path and file to be downloaded.
Very frustrated, any help appreciated!!
Thank,
Rob
 
Steve Leach
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used...

which works.
The only difference I can see is that I use "inline;" and you use "attachment;". Could that be it ?
 
Steve Leach
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noticed that you have a capital "D" for "Disposition". Maybe it is case-sensitive.
 
Jaap van Hengstum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's a caching problem. Try adding res.setHeader("Cache-Control", "public") to your servlet.
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaap van Hengstum:
Maybe it's a caching problem. Try adding res.setHeader("Cache-Control", "public") to your servlet.

Jaap,
You are the best! Struggled with this for days with no success, and your code fixed the problem.
Can you please explain what the caching problem was, so I can understand going forward.
Thanks again!!
Rob
 
Jaap van Hengstum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Levo:

Jaap,
You are the best! Struggled with this for days with no success, and your code fixed the problem.
Can you please explain what the caching problem was, so I can understand going forward.

I spent quite a few hours on a similar problem also, so I can understand
What I know is that, in my situation, when you are running your servlet from within a security-constraint or HTTPS site, the server (Tomcat in my case) will automatically send a "Cache-Control: no-cache" header with every page (for obvious security reasons). This causes the browser (IE in my case) to disable caching the pages, so when a file is downloaded it is not stored on the disk (unless you specifically request to save it to another file by choosing 'save' in the popup dialog instead of 'open') and in some cases the browser cannot even remember the filename (which might or might not be a bug in IE).
So the solution is to overwrite the Cache-Control header when using content-disposition to download or display a file by setting the header in the code. The Cache-Control header can be set to 'public' or 'private' and I think 'private' may actually be a better choice because, according to the documentation, this requests public proxies not to cache the file but only private caches like the browser cache. I tried this and it works in IE.
This is what I know about this issue.
[ March 18, 2004: Message edited by: Jaap van Hengstum ]
 
Sadaf Ahmed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You may not have known but the reply saved my life!!! I am now able to open documents/files from my jsp. The only prb now is that if the user clicks on the Open button on the Browser Dialog Box, the browser goes in some infinite loop as the progress bar gets stuck somewhere.
Have you faced this issue? Could you tell me the solution for this?
Regards,
Sadaf
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaap van Hengstum:

I spent quite a few hours on a similar problem also, so I can understand
What I know is that, in my situation, when you are running your servlet from within a security-constraint or HTTPS site, the server (Tomcat in my case) will automatically send a "Cache-Control: no-cache" header with every page (for obvious security reasons). This causes the browser (IE in my case) to disable caching the pages, so when a file is downloaded it is not stored on the disk (unless you specifically request to save it to another file by choosing 'save' in the popup dialog instead of 'open') and in some cases the browser cannot even remember the filename (which might or might not be a bug in IE).
So the solution is to overwrite the Cache-Control header when using content-disposition to download or display a file by setting the header in the code. The Cache-Control header can be set to 'public' or 'private' and I think 'private' may actually be a better choice because, according to the documentation, this requests public proxies not to cache the file but only private caches like the browser cache. I tried this and it works in IE.
This is what I know about this issue.
[ March 18, 2004: Message edited by: Jaap van Hengstum ]

Thanks for the explanation!!
Take care,
Rob
 
Chandima Rodrigo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-Try this out-
res.setHeader("Content-Disposition", "attachment;filename=\"file_to_download.exe\"");


- My Code -
response.setHeader("Content-Disposition", "INLINE; filename=\"MyFile.exe\"");

Originally posted by Rob Levo:
I understand there have been other messages regarding this topic, but I am having as problem I can not find the answer to, so please assist.

I am doing a simple binary download with the following code, and I am getting the wrong path for the file to be downloaded in the dialog that pops up in the browser.

exe_filename = "file_to_download.exe"
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename="+ exe_filename);

This code cause the dialog to pop up stating the wrong path and file to be downloaded.

Very frustrated, any help appreciated!!

Thank,
Rob
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!