我的 web 应用程序的用户应该能够下载超过 10MB 的 zip 文件。 我的 Controller 中有以下代码:
[HttpPost]
public IActionResult Download(){
Response.Clear();
esponse.Headers.Add("Content-disposition", "attachment; filename=" + filename +".zip");
Response.ContentType = "application/zip";
Response.SendFileAsync(pathOfTheFile);
Response.Body.FlushAsync();
return Content("Download successfull");
}
它开始下载文件,但下载停止并显示网络错误。 当我在下载栏中说继续时,它说没有文件。
我的问题是什么?
最佳答案
发生错误的原因是响应的 header 和内容被写入了两次。第一次是由您的代码(包括 Response.Body.FlushAsync)编写的,第二次是由 ASP.NET 框架在执行操作返回的结果时编写的(IActionResult. ExecuteResultAsync
)。
考虑使用Controller
提供的File
方法
[HttpPost]
public IActionResult Download()
{
byte[] content = System.IO.File.ReadAllBytes(pathOfTheFile);
return File(content, "application/zip", filename);
}
关于c# - 从 webapp 下载 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246206/