4

I want to return a .csv file from an ASP.NET MVC 3 ActionResult in the controller. I've found other examples of people doing this out on the web, but it seems like they have an ASP data structure that they are converting to csv.

My situation is a little different. I actually have a file on the local drive called myfile.csv. I want to return the file so the user can download it as a .csv.

Here is my code:

public ActionResult GetCSVFile()
    {
        return File(service.initiateCsvGeneration(1), "text/csv");
    }

initiateCsvGeneration returns a string with a path to the filename. My filename already has the .csv extension and it is already in a comma delimited format. For some reason it is chopping of the .csv extension in my filename. Instead of allowing the user to download it as myfile.csv, it makes them download it as myfile although the actual disk file has the .csv extension and it is also present in the string that is in initiateCsvGeneration.

What else could be wrong here?

1 Answer 1

4

You should explicitly specify the desired file name - then your users will be able to see it in download dialog:

return File(service.initiateCsvGeneration(1), "text/csv", "myfile.csv");
Sign up to request clarification or add additional context in comments.

1 Comment

@ Andrei Thanks for your answer. Do you have Idea how to return multiple csv files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.