简体   繁体   中英

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?

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.

The following class adds a file DownloadResult to your program:

public class DownloadResult : ActionResult
{

    public DownloadResult()
    {
    }

    public DownloadResult(string virtualPath)
    {
        this.VirtualPath = virtualPath;
    }

    public string VirtualPath { get; set; }

    public string FileDownloadName { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (!String.IsNullOrEmpty(FileDownloadName))
        {
            context.HttpContext.Response.AddHeader("content-disposition",
              "attachment; filename=" + this.FileDownloadName);
        }

        string filePath = context.HttpContext.Server.MapPath(this.VirtualPath);
        context.HttpContext.Response.TransmitFile(filePath);
    }
}

To call it, do something like this in your controller method:

public ActionResult Download(string name)
{
    return new DownloadResult 
       { VirtualPath = "~/files/" + name, FileDownloadName = name };
}

Note the virtual path, which is a files directory in the root of the site; this can be changed to whatever folder you want. This is where you put your files for download. Check this tutorial about Writing A Custom File Download Action Result For ASP.NET MVC

Another simplified example using FileResult as takepara suggested above.

Note I created a HelperController.cs class.

In your view...

@Html.ActionLink("Link Description", "Download", "Helper", new { fileName = "downloaded_file_name.ext", path = "root path location to your file" }, null)


Controller action...

public FileResult Download(string fileName, string path)
{
    var webRootPath = Server.MapPath("~");
    var documentationPath = Path.GetFullPath(Path.Combine(webRootPath, path));
    var filePath = Path.GetFullPath(Path.Combine(documentationPath, fileName));

    return File(filePath, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM