C# 从web api返回zipfile

C# 从web api返回zipfile,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我已经建立了一个asp网络api。我需要返回一个zipfile,这是一些内部逻辑的结果。我正在使用这段代码,它可以正常工作,但是手动解压缩后生成的zip文件给了我一个错误“负载结束后有数据” 这是我在.net控制台应用程序中接收数据的方式: using (Stream output = File.OpenWrite(@"C:\prova\MyFile.zip")) using (Stream input = httpResponse.GetResponseStream()) {

我已经建立了一个asp网络api。我需要返回一个zipfile,这是一些内部逻辑的结果。我正在使用这段代码,它可以正常工作,但是手动解压缩后生成的zip文件给了我一个错误“负载结束后有数据”

这是我在.net控制台应用程序中接收数据的方式:

using (Stream output = File.OpenWrite(@"C:\prova\MyFile.zip"))
    using (Stream input = httpResponse.GetResponseStream())
    {
        input.CopyTo(output);
    }

如果系统中已经有zip文件,那么在将其作为响应发送之前,不需要做任何特殊的操作

这应该起作用:

string filePath = @"C:\myfolder\myfile.zip";

return File(filePath, "application/zip");
public IActionResult GetZipFile(){

   //location of the file you want to compress
   string filePath = @"C:\myfolder\myfile.ext";

   //name of the zip file you will be creating
   string zipFileName = "zipFile.zip";

   byte[] result;


   using (MemoryStream zipArchiveMemoryStream = new MemoryStream())
   {
       using (ZipArchive zipArchive = new ZipArchive(zipArchiveMemoryStream, ZipArchiveMode.Create, true))
       {
           ZipArchiveEntry zipEntry = zipArchive.CreateEntry(zipFileName);
           using (Stream entryStream = zipEntry.Open())
           {
               using (MemoryStream tmpMemory = new MemoryStream(System.IO.File.ReadAllBytes(filePath)))
               {
                    tmpMemory.CopyTo(entryStream);
                };
           }
       }

       zipArchiveMemoryStream.Seek(0, SeekOrigin.Begin);
       result = zipArchiveMemoryStream.ToArray();
   }

   return File(result, "application/zip", zipFileName);

}
如果您正在动态创建文件,即获取其他文件并通过编程将其放入用户的zip文件中,则以下操作应有效:

string filePath = @"C:\myfolder\myfile.zip";

return File(filePath, "application/zip");
public IActionResult GetZipFile(){

   //location of the file you want to compress
   string filePath = @"C:\myfolder\myfile.ext";

   //name of the zip file you will be creating
   string zipFileName = "zipFile.zip";

   byte[] result;


   using (MemoryStream zipArchiveMemoryStream = new MemoryStream())
   {
       using (ZipArchive zipArchive = new ZipArchive(zipArchiveMemoryStream, ZipArchiveMode.Create, true))
       {
           ZipArchiveEntry zipEntry = zipArchive.CreateEntry(zipFileName);
           using (Stream entryStream = zipEntry.Open())
           {
               using (MemoryStream tmpMemory = new MemoryStream(System.IO.File.ReadAllBytes(filePath)))
               {
                    tmpMemory.CopyTo(entryStream);
                };
           }
       }

       zipArchiveMemoryStream.Seek(0, SeekOrigin.Begin);
       result = zipArchiveMemoryStream.ToArray();
   }

   return File(result, "application/zip", zipFileName);

}

这是我自己最近的一个ASP.NET项目。

非常感谢您的回复。如果我使用:返回文件(di.FullName+“\\Update.zip”,“application/zip”);VS告诉我不可能使用“文件”,它不是callable@Swinz您使用的是什么版本的ASP?您可能需要玩弄返回类型。在我当前的webAPI项目中,我有另一个可能的选项
返回PhysicalFile(字符串文件名,字符串内容类型)
。我使用的是asp net 4(.net framework 4.6)的webAPI模板PhysicalFile不起作用,我无法解决名称空间问题我已使用以下代码解决了问题:HttpResponseMessage response=new HttpResponseMessage(HttpStatusCode.OK);response.Content=newstreamcontent(newfilestream(path,FileMode.Open,FileAccess.Read));response.Content.Headers.ContentDisposition=new System.Net.Http.Headers.ContentDispositionHeaderValue(“附件”);response.Content.Headers.ContentDisposition.FileName=“Update.zip”;response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/zip”);