Change the document type using MIME

June 11, 2011 - 06:35

When any webpage is returned back to the browser a type is specified which indicates what type of document is being returned. Normally this would be a standard html document and this is rendered in the browser.

Sometimes you may wish to change this, for example you have used your asp.net webpage to dynamically create a csv file. Instead of this being displayed in the web browser you would want the user to have a download this document dialog and then associate this with the correct application to open.

To do this for word documents simply add the code onto the end of your page.

Response.AddHeader("Content-Type","application/msword")
Response.AddHeader("Content-disposition", "attachment; filename=mydocument.doc" )


For CSV files which would normally open in Excel use.

Response.AddHeader("Content-Type","text/csv")
Response.AddHeader("Content-disposition", "attachment; filename=mydocument.csv" )


There are a number of different MIME types you can use for the various types of documents which you can find by searching google.



© 2011 simplevb.net