How to download a file in ASP.NET Core

后端 未结 9 1992
离开以前
离开以前 2020-12-02 13:14

In MVC, we have used following code to download a file. In ASP.NET core, how to achieve this?

HttpResponse response = HttpContext.Current.Response;                   


        
9条回答
  •  悲哀的现实
    2020-12-02 13:34

    You can try below code to download the file. It should return the FileResult

    public ActionResult DownloadDocument()
    {
    string filePath = "your file path";
    string fileName = ""your file name;
    
    byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
    
    return File(fileBytes, "application/force-download", fileName);
    
    }
    

提交回复
热议问题