开发者

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

开发者 https://www.devze.com 2022-12-13 16:40 出处:网络
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

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:/开发者_StackOverflow/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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号