【问题标题】:Returning a zipfile from a web api从 web api 返回一个 zipfile
【发布时间】:2019-10-14 16:46:52
【问题描述】:

我已经构建了一个 asp net web api。由于某些内部逻辑,我需要返回一个 zipfile。我正在使用此代码并且它可以工作,但是生成的 zip 文件在手动解压缩时给了我这个错误“有效负载结束后有数据”

 using (ZipFile zip = new ZipFile())
 {
     ...
     zip.Save(di.FullName + "\\" + "Update.zip");
 }


 string path = Path.Combine(Properties.Settings.Default.PathDisposizioniHTML, "Update.zip");

 var response = new HttpResponseMessage(HttpStatusCode.OK);
 var stream = new System.IO.FileStream(path, System.IO.FileMode.Open);
 response.Content = new StreamContent(stream);
 response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

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

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

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api


    【解决方案1】:

    如果您的系统上已有该 zip 文件,则在将其作为响应发送之前无需执行任何特殊操作。

    这应该可行:

    string filePath = @"C:\myfolder\myfile.zip";
    
    return File(filePath, "application/zip");
    

    如果您正在即时制作文件,即获取其他文件并以编程方式将它们放入用户的 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 项目。

    【讨论】:

    • 非常感谢您的回复。如果我使用: return File(di.FullName + "\\Update.zip", "application/zip"); VS 告诉我不能使用“文件”,它是不可调用的
    • @Swinz 您使用的是什么版本的 ASP?您可能需要玩弄返回类型。在我目前的 webAPI 项目中,我有 return PhysicalFile(string fileName, string contentType) 作为另一个可能的选择。
    • 我用的是asp net 4(.net framework 4.6)的webapi模板
    • PhysicalFile 不起作用,我无法解析命名空间
    • 我已经用以下代码解决了这个问题:HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new FileStream(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 = new MediaTypeHeaderValue("application/zip");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2015-01-05
    • 2021-07-06
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多