Download file to browser using .NET Core Razor Pages

后端 未结 1 1593
独厮守ぢ
独厮守ぢ 2021-02-06 09:00

Using ASP.NET Razor Pages, I am trying download a file to the browser. From the Page(html), using a link like this works fine:

href=\"/DownloadableFiles/testB.cs         


        
相关标签:
1条回答
  • 2021-02-06 09:14

    pitaridis is correct, return File exists in Razor Pages, I must have been missing a namespace. This will download a file from Code Behind:

    In the page, here's the submit button:

    <button type="submit" asp-page-handler="DownloadFile" style="width:75px" 
            class="cancel"> Download </button>
    

    In the PageModel (code behind):

    public ActionResult OnPostDownloadFile()
    {
        return File("/DownloadableFiles/TestFile34.csv", "application/octet-stream", 
                    "NewName34.csv");
    }
    

    Note: /DownloadableFiles is in a subfolder of wwwroot

    0 讨论(0)
提交回复
热议问题