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

Forcing filetype on download  RSS feed

 
Barry Gold
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a servlet to send a binary file for the user to download.

Here are the headers I'm sending:

resp.addHeader("Content-Type", "application/octet-stream");
// Prevent caching
resp.addHeader("Cache-Control", "no-cache, must-revalidate");
resp.addHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
// prefer download instead of in-browser view
resp.addHeader("Content-Disposition", "attachment; filename=$extfname");
// fsize is the filesize as reported by File.length()
resp.addIntHeader("Content-Length", fsize);

But the dialog from my browser says:

You have chosen to open
(icon) filename
which is a: Hypertext Document
from: (my site)

I want it to say "binary file" or "application file" -- maybe even the real filetype -- instead of "Hypertext Document" which it definitely isn't.

Any suggestions?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MSIE sometimes ignores the content type header and tries to determine the file type by the extension.

What extension is the extfname var returning?
Also, why the preceding '$' sign.
 
Barry Gold
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, the $ is a typo.

The extname var contains the "external filename" -- usually the name of the file when it was first uploaded -- which includes the correct extension (.pdf, .doc, .wmv, etc.)

If it were guessing the filetype based on the extension, it would get it right. But the filename is being shown correctly on the dialog (with the proper extension), only the displayed filetype is wrong.

And the problem occurs in both MSIE and Firefox.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try setting the content type to x-application/octet-stream
 
Barry Gold
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did. No joy.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...
If you haven't already, install the LiveHTTPHeaders plugin for Mozilla/Firefox.

Then, if you like record the TCP/IP stack and post it.
I'd be interested to see what's being sent to the browser.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
resp.addHeader("Content-Disposition", "attachment; filename=$extfname");


The above statement isn't going to set the file name correctly. Is this actually what's in your code? I realize you said the $ was a typo, but the code should be:

resp.addHeader("Content-Disposition", "attachment; filename="+extfname);
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!