Have some audio and video files that users are to download, however depending on the file type or browser the browser may attempt to play the file instead of downloading it. This is not desired, how can I avoid this? The anchor will be a direct link to the file unless I need to create some sort of Action to handle this properly. I am using C# ASP.NET MVC.
views:
28answers:
2
+2
A:
You'll have to set the Content-Disposition HTTP header to 'attachment':
Content-Disposition: attachment; filename="file.ext"
Nev Stokes
2010-09-09 15:49:23
+2
A:
The important parts are setting the response headers. Set both the content-type header and the content-disposition header. Here is an example:
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.AddHeader("Content-Length", lenOfFile)
Response.ContentType = "application/octet-stream"
Stephen M. Redd
2010-09-09 15:53:56
good call on the length of file header
shogun
2010-09-09 16:02:23
yeah, need that content length... it is used by the progress bars in most downloaders.
Stephen M. Redd
2010-09-09 16:29:20
Good call on application/octet-stream - this will prompt an open/save/cancel dialog.
code4life
2010-09-09 17:24:10