Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the file unless I need to create some sort of Action to handle this properly. I am using C# ASP.NET MVC.

like image 886
MetaGuru Avatar asked Nov 19 '25 21:11

MetaGuru


2 Answers

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" 
like image 173
Stephen M. Redd Avatar answered Nov 22 '25 11:11

Stephen M. Redd


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

Content-Disposition: attachment; filename="file.ext"
like image 45
Nev Stokes Avatar answered Nov 22 '25 13:11

Nev Stokes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!