How to make a video or audio file HTTP response always download instead of play in browser?
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 t开发者_StackOverflow中文版he file unless I need to create some sort of Action to handle this properly. I am using C# ASP.NET MVC.
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"
You'll have to set the Content-Disposition HTTP header to 'attachment':
Content-Disposition: attachment; filename="file.ext"
精彩评论