Sometimes when I'm dynamically generating a graphic on the server side, perhaps a chart or graph, or I'm retrieving a check image via Web Services, the user wants to save the time, and I want to control the file name that is suggested in the File|Save As Dialog. Most often the browser will just recommend the page's file name (the one that was part of the HTTP GET request). So, how do you control the suggested Save As filename?
UPDATE: Here's how you do it in IE8.
Well, you do and you don't. There's a HTTP Header called Content-Disposition (case senstitive) that is NOT part of the HTTP standard, but rather it's own Request for Comments, RFC 1806.
HTTP Headers are name values pairs, so they are easily added with the Response object in ASP or ASP.NET You use it like this (the HTTP Headers):
HTTP/1.1 200 OK<snip>Content-Disposition: filename=checkimage.jpgContent-Length: 76127Content-Type: image/JPEG
HTTP/1.1 200 OK<snip>Content-Disposition: attachment; filename=checkimage.jpgContent-Length: 76127Content-Type: image/JPEG
Here's a list of gotchas, starting with my own:
Also, there are some security issues around Content-Disposition, like suggesting evil names of files to make a careless user overwrite /etc/passwd, etc, that were included in the updated RFC 2183.
For classic ASP folks, here are some good code samples around Content-Disposition.
It's a shame that this can't just work as the spec was written, and it's taken 2+ versions of IE to still get it wrong.
Note: Remember, if you want to sniff your own HTTP Headers, do it with class and use Jonas Blunck's iehttpheaders.
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.