Gautam Jain Gautam Jain -4 years ago 276
ASP.NET (C#) Question

How do I link to downloadable files in ASP.NET MVC?

I am new to ASP.NET MVC and I'am trying to link to downloadable files (.zip, .mp3, .doc, etc).

I have the following view:


which maps to:

I have a
file that must map to URL


Where do I place this
file in the MVC folder structure?

How do I add link to this
file in MVC? Is there a Url.* method that do this?

Answer Source

You can use FilePathResult or Controller.File method.

protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName) {
  return new FilePathResult(fileName, contentType) { FileDownloadName = fileDownloadName };

Sample code action method.

public ActionResult Download(){
  return File(fileName,contentType,downloadFileName);

Hope this code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download