tags:

views:

1698

answers:

4

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?

+1  A: 

I don't think it works that way.

Use a Content-Disposition: attachment header, but stick with the correct Content-Type.

Tomalak
+11  A: 

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).

Andrew Moore
Do you need to lie about the Content-type? Would be nicer if you could just use Content-Disposition, which /should/ work.
Bobby Jack
In my experience, application/octet-stream makes it work more reliably across all browsers.
ceejayoz
^ What he said. Internet Explorer 6 may still display it as text if text/plain is used.
Andrew Moore
A: 

Here is a list of ASP.net examples. It is most reliable with both an application/octet-stream content type and a content-disposition header of 'attachment'.

ceejayoz
+2  A: 

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\"
Mun