Write to the Http Response to download data as a file
How to create and return a csv file for the user to download from a webpage.
Use the Http Response object to create a text/csv file
This example creates csv data in a string buffer to download as a csv file. In your MVC controller method, set the Response properties to indicate the Content Type as text/csv, set the content length and file name that will be downloaded to the user's browser as an attachment.
Write the string data to the response, then close and end the response.
The user will see a csv file download in their browser. When they open the csv file, their associated program (such as Excel) will open the file.
var strExport = "name, address, city";
Response.ContentType = "text/csv";