简体   繁体   English

如何从httpResponseMessage读取zipfile作为StreamContent?

[英]How to read zipfile as StreamContent from httpResponseMessage?

I send a zipfile as a response.Content: 我发送一个zip文件作为响应。

[HttpGet]
[Route("Package")]
public async Task<HttpResponseMessage> GetLogsPackage()
{
   HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);                     
   using (var stream = new MemoryStream())
   {
       using (var zipFile = ZipFile.Read((Path.Combine(path, opId.ToString()) + ".zip")))                
       {
           zipFile.Save(stream);
           response.Content = new StreamContent(stream);
           response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
           response.Content.Headers.ContentLength = stream.Length;
       }
   }
   return response;
}

how to get this stream after call to this method? 调用此方法后如何获得此流? My code doesn't work( can't read as zipfile) I send stream.lenght ,for example, 345673, but receive response with 367 lenght. 我的代码不起作用(无法读取为zipfile),我发送stream.lenght,例如345673,但收到的响应长度为367。 What is wrong? 怎么了?

  var response = await _coreEndpoint.GetLogsPackage();
  using (var stream = response.Content.ReadAsStreamAsync())
  using (var zipFile = ZipFile.Read(stream))
   {   //do something with zip-file

Looks like you should be await 'ing ReadAsStreamAsync : 看起来您应该在await ReadAsStreamAsync

using (var stream = await response.Content.ReadAsStreamAsync())

Currently your code is passing a Task<Stream> to ZipFile.Read , which is probably not what you intend. 当前,您的代码正在将Task<Stream>传递给ZipFile.Read ,这可能不是您想要的。

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

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