I have an Action which is responsible for pulling some data out from the database and returning it as a CSV file.
I know I could do it by returning a massive string but I was thinking if there is a clean and efficient way that could be used to return a CSV file through an MVC controller action.
The FileResult is what you are looking for:
http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx
public ActionResult GetFile()
{
var stream = new StreamReader("thefilepath.txt");
return File(stream.ReadToEnd(), "text/plain");
}
Best regards
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