简体   繁体   English

从 webapp 下载 zip 文件

[英]Download zip file from webapp

The user of my webapp should be able to download a zip-file with more than 10MB.我的 web 应用程序的用户应该能够下载超过 10MB 的 zip 文件。 I have the following code in my 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");
}

It starts downloading the file but the download stops an it says network error.它开始下载文件,但下载停止并显示网络错误。 When I say continue in the download-bar, it says that there is no file.当我在下载栏中说继续时,它说没有文件。

What is my problem?我的问题是什么?

The error occurs because response with headers and content is written twice.发生错误是因为带有标头和内容的响应被写入两次。 First time it's written by your code including Response.Body.FlushAsync and second time it's written by ASP.NET framework when result returned by action is executed ( IActionResult.ExecuteResultAsync ).第一次由您的代码编写,包括Response.Body.FlushAsync ,第二次由ASP.NET框架在执行操作返回的结果 ( IActionResult.ExecuteResultAsync ) 时IActionResult.ExecuteResultAsync

Consider using File method provided by Controller考虑使用Controller提供的File方法

[HttpPost]
public IActionResult Download()
{
    byte[] content = System.IO.File.ReadAllBytes(pathOfTheFile);

    return File(content, "application/zip", filename);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM