What content type to force download of text response?
I need to stream a file to the Response for saving on the end user's machine. The file is plain text, so what content type can I use to prevent the text being displayed in the browser?
Solution 1:
To be on the safe side and ensure consistent behavior in all browsers, it's usually better to use both:
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"
Solution 2:
In most cases, the following should work:
Content-type: application/octet-stream
Content-Disposition: attachment; filename="myfile.txt"
There are some marginal cases of browsers that will still display it as a text file, but none of the mainstream browsers will (I'm talking about browsers embedded in some MIDs).
EDIT: When this answer was originally published, sending the Mime-Type application/octet-stream
was the only reliable way to trigger a download in some browsers. Now in 2016, if you do not need to support an ancient browser, you can safely specify the proper mime-type.