1

我发送一个压缩文件作为响应。内容:

[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
4

1 回答 1

0

看起来你应该await'ing ReadAsStreamAsync

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

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

于 2017-08-18T07:23:58.993 回答