代码之家  ›  专栏  ›  技术社区  ›  Ayush

如何将HttpResponse StreamContent转换为zip文件?

  •  0
  • Ayush  · 技术社区  · 7 年前

    我有一个API正在返回 HTTPResponse 内容类型为“流内容”。现在,我想在另一个应用程序中使用此API,并下载作为流内容发送的zip文件。有人能建议我如何在C语言中实现这一点吗?

    private HttpResponseMessage GetHttpResponseMessageForConfigFile(Component component)
    {
        var result = Request.CreateResponse(HttpStatusCode.OK);
        component.ConfigData = _configDataService.GetConfigDetail(component.Id).Data;
        var mermoryStream = new MemoryStream(component.ConfigData);
        result.Content = new StreamContent(mermoryStream);
    
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = string.Format("{0}.zip", component.Code)
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
        return result;
    }
    

    在上述内容中, component.ConfigData 包含zip文件的字节数组。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Thomas Weller    7 年前

    您也可以像下载任何其他二进制文件一样下载ZIP文件:

    using (WebClient client = new WebClient()) 
    {
        client.DownloadFile(new Uri(url), @"c:\path\to\file.zip");
    }