我找不到使用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也是正确的