Download File using MVC Core

前端 未结 2 1341
小蘑菇
小蘑菇 2020-12-09 09:28

I cannot find a reference to downloading a file using MVC Core.

We have a single exe file for members to download from our website. In the past we have put

<

相关标签:
2条回答
  • 2020-12-09 10:10

    I used this answer posted by @Tieson T to come up with this solution

        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);
        }
    

    The view is now

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

    @Ageonix was also correct about not requiring the ~ to get to wwwroot

    0 讨论(0)
  • 2020-12-09 10:10

    I'm not somewhere where I can try it, but would something like this do the trick?

    <a href="<%= Url.Content('~/Downloads/{ V9.Version}.exe') %>"> Download </a>
    
    0 讨论(0)
提交回复
热议问题