c# - 从 webapp 下载 zip 文件
问题描述
我的 webapp 的用户应该能够下载超过 10MB 的 zip 文件。我的控制器中有以下代码:
[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");
}
它开始下载文件,但下载停止并显示网络错误。当我在下载栏中说继续时,它说没有文件。
我的问题是什么?
解决方案
发生错误是因为带有标题和内容的响应被写入了两次。第一次是由您的代码编写的Response.Body.FlushAsync
,第二次ASP.NET
是在执行动作返回的结果时由框架编写的(IActionResult.ExecuteResultAsync
)。
考虑使用File
提供的方法Controller
[HttpPost]
public IActionResult Download()
{
byte[] content = System.IO.File.ReadAllBytes(pathOfTheFile);
return File(content, "application/zip", filename);
}
推荐阅读
- docker - docker compose up 工作正常,但无法访问显示站点的浏览器
- python - 使用inplace = True时如何在控制台上打印?
- javascript - message.guild.channels.find 不是函数
- java - 无法在休眠 Spring Boot 中的单个事务中保存父项和子项
- r - 基于逻辑列对复杂稀疏矩阵进行子集化
- ios - 如果我对搜索结果使用单独的数组,如何更新两个数组中的对象?
- javascript - Slack Bot 与 Bolt for JavaScript 和 OAuth 2.0 与其他工作区共享应用程序
- java - 如何使用spring boot在浏览器的单页中呈现多个文档
- python - 如何根据每行的长度合并excel中的行(一行中有数据的单元格数)
- javascript - 为什么 javascript 不检测在 django for 循环之后添加的元素?