How to download a file in ASP.NET Core

后端 未结 9 1993
离开以前
离开以前 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:45

    Your controller should return an IActionResult, and use the File method, such as this:

    [HttpGet("download")]
    public IActionResult GetBlobDownload([FromQuery] string link)
    {
        var net = new System.Net.WebClient();
        var data = net.DownloadData(link);
        var content = new System.IO.MemoryStream(data);
        var contentType = "APPLICATION/octet-stream";
        var fileName = "something.bin";
        return File(content, contentType, fileName);
    }
    

提交回复
热议问题