I need to send a CSV file in HTTP response. How can I set the output response as CSV format?
This is not working:
Response.ContentType = "application/CSV";
A CSV (comma-separated values) file is a text file that has a specific format which allows data to be saved in a table structured format.
A CSV file is a list of data separated by commas. For instance, it may look like the following: Name,email,phone number,address. Example,[email protected],555-555-5555,Example Address.
If a server says "This data is of type text/csv" the client can understand that can render that data internally, while if the server says "This data is of type application/csv" the client knows that it needs to launch the application that is registered on the OS to open csv files.
Using text/csv
is the most appropriate type.
You should also consider adding a Content-Disposition
header to the response. Often a text/csv will be loaded by a Internet Explorer directly into a hosted instance of Excel. This may or may not be a desirable result.
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
The above will cause a file "Save as" dialog to appear which may be what you intend.
MIME type of the CSV is text/csv
according to RFC 4180.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With