如何从httpResponseMessage读取zip文件作为StreamContent?

时间:2017-08-18 07:11:06

标签: c# zipfile httpresponsemessage

我发送一个zipfile作为回应。内容:

[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;
}

调用此方法后如何获取此流?我的代码不起作用(不能读为zipfile) 我发送stream.lenght,例如345673,但收到367长度的响应。 有什么问题?

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

1 个答案:

答案 0 :(得分:0)

您应该await ReadAsStreamAsync

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

目前,您的代码正在将Task<Stream>传递给ZipFile.Read,这可能不是您想要的。