简体   繁体   中英

How to read zipfile as StreamContent from httpResponseMessage?

I send a zipfile as a response.Content:

[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. 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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