Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Action returning a CSV File

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.

like image 348
Whiz Kid Avatar asked May 22 '26 17:05

Whiz Kid


1 Answers

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

like image 56
Oscar Avatar answered May 25 '26 11:05

Oscar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!