How can I set a file (such as a text file) to be loaded in the browser instead of downloaded?

Helena Grulichova asked on May 31, 2012 03:48

How can I set a file (such as a text file) to be loaded in the browser instead of downloaded?

Correct Answer

Helena Grulichova answered on May 31, 2012 03:48

For some files, the default behavior in IIS is to set the content-disposition to “attachment”. You can do this by adding a rewrite rule to your web.config. The following should work:
 
<rewrite>
            <outboundRules>
                <rule name="remove cd">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern="attachment; filename=.+txt$" />
                    <action type="Rewrite" />
                </rule>
           </outboundRules>
</rewrite>


This removes the content-disposition header for txt files and should cause the file to be loaded inline, assuming the client’s browser is not overriding the default behavior for the mime type.

Note: You may need to install the URL rewrite module to your IIS.
 
-ag-
0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.