Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from ASP.NET MVC 2 site

I'm currently trying to implement a controller where you can download files from (more specifically jar-archives). The files are stored on disk and not in database. So far I have come up with this:

public FilePathResult GetFile(String fileName) 
{    
   return File(Path.Combine(Server.MapPath("~/App_Data/Bundles"), fileName), "application/java-archive");
}

Nevermind the lack of error handling and such at this time. The file do get downloaded this way, however it gets the wrong name. Instead of, for example, "sample.jar" the file gets the controller's name, "GetFile", (without extension).

Any ideas on what I am doing wrong?

like image 596
Fredrik Wallenius Avatar asked Dec 22 '22 17:12

Fredrik Wallenius


1 Answers

Use the overload which allows you to specify the fileDownloadName.

return File(Path.Combine(Server.MapPath("~/App_Data/Bundles"), fileName),
            "application/java-archive", 
            fileName);
like image 99
Craig Stuntz Avatar answered Jan 05 '23 09:01

Craig Stuntz