asp.net-core-mvc – 使用MVC Core下载文件

前端之家收集整理的这篇文章主要介绍了asp.net-core-mvc – 使用MVC Core下载文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我找不到使用MVC Core下载文件的参考.

我们有一个exe文件供会员从我们的网站下载.在过去我们已经把

< a href =(文件路径)>下载< / A>供我们的用户点击.我想在MVC Core中做同样的事情

<a href=@ViewData["DownloadLink"]> Download < /a>

使用下载链接填充文件路径.

public class DownloadController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {
        ViewData["DownloadLink"] = ($"~/Downloads/{V9.Version}.exe");
        return View();
    }
}

`

链接< a href = @ViewData [“DownloadLink”]>下载< / A>获取正确的路径,但单击时只在地址栏中呈现路径.有没有简单的方法来设置下载链接

解决方法

我使用@Tieson T发布的 this回答来提出这个解决方
public FileResult Download()
    {
        var fileName = $"{V9.Version}.exe";
        var filepath = $"Downloads/{fileName}";
        byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
        return File(fileBytes,"application/x-msdownload",fileName);
    }

现在是观点

<a asp-action="Download" asp->
Download

@Ageonix对于不要求〜来到wwwroot也是正确的

猜你在找的asp.Net相关文章