Asp.net-Core-Mvc
使用 MVC Core 下載文件
我找不到使用 MVC Core 下載文件的參考。
我們有一個 exe 文件供會員從我們的網站下載。過去我們已經把
<a href=(file path)> Download < /a>供我們的使用者點擊。我想在 MVC Core 中做一些等效的事情<a href=@ViewData["DownloadLink"]> Download < /a>使用文件路徑填充 DownloadLink。
public class DownloadController : Controller { [HttpGet] public IActionResult Index() { ViewData["DownloadLink"] = ($"~/Downloads/{V9.Version}.exe"); return View(); } }`
該連結
<a href=@ViewData["DownloadLink"]> Download < /a>獲取正確的路徑,但點擊時僅在地址欄中呈現路徑。有沒有簡單的方法來設置下載連結?
我使用@Tieson T 發布的這個答案來提出這個解決方案
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 也是正確的