ASP.Net Core Web API/.NET 6应用程序中如何下载文件到本地呢?
3.24K 次浏览
在ASP.NET MVC中,我们使用以下代码从服务器下载一个文件到本地。在ASP.NET Core,ASP.NET Core Web API,.NET 6等应用程序中,又如何实现将服务器文件下载到本地的功能呢?
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();