views:

28

answers:

2

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.

+2  A: 

You'll have to set the Content-Disposition HTTP header to 'attachment':

Content-Disposition: attachment; filename="file.ext"
Nev Stokes
+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
good call on the length of file header
shogun
yeah, need that content length... it is used by the progress bars in most downloaders.
Stephen M. Redd
Good call on application/octet-stream - this will prompt an open/save/cancel dialog.
code4life