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:

ProductName

which maps to:
http://domain/ProductName


I have a
.zip
file that must map to URL
http://domain/ProductName/Product.zip


Questions



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

How do I add link to this
.zip
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