在 MVC 中,我们使用以下代码来下载文件。在ASP.NET core中,如何实现这一点?
HttpResponse response = HttpContext.Current.Response;
System.Net.WebClient net = new System.Net.WebClient();
string link = path;
response.ClearHeaders();
response.Clear();
response.Expires = 0;
response.Buffer = true;
response.AddHeader("Content-Disposition", "Attachment;FileName=a");
response.ContentType = "APPLICATION/octet-stream";
response.BinaryWrite(net.DownloadData(link));
response.End();
你的控制器应该返回一个IActionResult
,并使用File
方法,比如这样:
[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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)