ASP.NET MVC 3 Return CSV file from controller

I want to return the CSV file from ASP.NET MVC 3 ActionResult to the controller. I found other examples of people who do this on the Internet, but it looks like they have an ASP data structure that they convert to csv.

My situation is a little different. Actually, I have a file on the local drive named myfile.csv. I want to return the file so that the user can upload it as .csv.

Here is my code:

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

initiateCsvGeneration returns a string indicating the path to the file name. My file name already has a .csv extension, and it is already in a comma-separated format. For some reason, this interrupts the .csv extension in my file name. Instead of allowing the user to download it as myfile.csv, it forces him to download it as myfile, although the actual file on the disk has the extension .csv and is also present in the line located in initiateCsvGeneration.

What else could be wrong?

+4
source share
1 answer

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

 return File(service.initiateCsvGeneration(1), "text/csv", "myfile.csv"); 
+4
source

Source: https://habr.com/ru/post/1484392/


All Articles