如何将 GZipStream 转换为 HttpContent?

我编写了一个 Web API 代码来返回一个 zip 文件。但我无法将 GZipStream 内容转换为 HttpContent。我收到以下错误:


无法将类型“system.io.compression.GZipStream”隐式转换为“System.Net.Http.HttpContent”


我哪里做错了?


我的 WebApi 代码:


var content = 

    new GZipStream(memStream, CompressionMode.Compress);


HttpResponseMessage httpResponseMessage = new HttpResponseMessage();

httpResponseMessage.Content = content;

httpResponseMessage.Content.Headers.Add("x-filename", document.FileName);

httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");

httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

httpResponseMessage.Content.Headers.ContentDisposition.FileName = "xyz.zip";

httpResponseMessage.StatusCode = HttpStatusCode.OK;


慕雪6442864
浏览 200回答 2
2回答

隔江千里

我得到了一个使用 Zip 存档的解决方案,我创建了一个静态类来将内存流推送到一个 zip 存档中,如下所示,&nbsp;var pushStreamContent = ZipStreamContent.Create("MultipleDocument.zip", memStList);ZipStreamContent 类,public static class ZipStreamContent{&nbsp; &nbsp; public static PushStreamContent Create(string fileName, List<MemoryStream> msList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var content = new PushStreamContent((outputStream, httpContent, transportContext) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var zip = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: false))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msList[0].Position = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var createenter = zip.CreateEntry("xyz.jpg", CompressionLevel.Optimal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var s = createenter.Open())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msList[0].CopyTo(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");&nbsp; &nbsp; &nbsp; &nbsp; content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");&nbsp; &nbsp; &nbsp; &nbsp; content.Headers.ContentDisposition.FileName = fileName;&nbsp; &nbsp; &nbsp; &nbsp; return content;&nbsp; &nbsp; }}

九州编程

我已经简化了上面给出的 GipStreamContent 静态类,如下所示,它运行良好,所以我希望它能帮助所有其他人。&nbsp; &nbsp;CloudBlockBlob blob = null;&nbsp; &nbsp;//azure storage connection&nbsp; &nbsp;var container = GetBlobClient(tenantInfo);&nbsp; &nbsp;//directory reference&nbsp; &nbsp;var directory = container.GetDirectoryReference(&nbsp; &nbsp;string.Format(DirectoryNameConfigValue, tenantInfo.TenantId.ToString(), documentList[0].ProjectId));&nbsp; var pushStreamContent = new PushStreamContent(async (outputStream, httpContent, transportContext) =>&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //zip the multiple files&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var zipEntry = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: false))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int docId = 0; docId < documentList.Count; docId++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blob = directory.GetBlockBlobReference(DocumentNameConfigValue + documentList[docId].DocumentId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!blob.Exists()) continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MemoryStream memStream = new MemoryStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await blob.DownloadToStreamAsync(memStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memStream.Position = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var createEntry = zipEntry.CreateEntry(documentList[docId].FileName, CompressionLevel.Fastest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var stream = createEntry.Open())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memStream.CopyTo(stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;});&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP