简体   繁体   English

如何从 HttpResponseMessage 读取应用程序/pdf 内容类型并将其转换为 stream

[英]How to read application/pdf content type from HttpResponseMessage and convert it into stream

I have a Web API which connects to another end point.我有一个 Web API 连接到另一个端点。 A GET method from the second Web API returns an application/pdf content-type.来自第二个 Web API 的 GET 方法返回应用程序/pdf 内容类型。 here I am successfully getting the data.在这里,我成功获取了数据。 How I can convert the response.Content to file/memory stream and return the value?如何将 response.Content 转换为文件/内存 stream 并返回值?

 public async Task<T> functionname<T>(ApplicationApiRoutes.APIRoutes route, string token = "", int TimeoutMinutes = 5, CancellationToken cancellationToken = default, params string[] routeparams)
    {
        T Response = default;
        try
        {
            ApplicationApiRoutes._RouteHashTable.TryGetValue(route, out string FormattedRoute);

            FormattedRoute = String.Format(FormattedRoute, routeparams);

            using (var request = new HttpRequestMessage(HttpMethod.Get, $"{FormattedRoute}"))
            {
                if (!String.IsNullOrWhiteSpace(token))
                {
                    request.Headers.Add("Cookie", $"access_token="+ token);
                }
                
               

                using (var response = await _httpclient
                    .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
                {

                    
                   
                    response.EnsureSuccessStatusCode();

                    var result = await response.Content.ReadAsStringAsync();

                    Response = JsonConvert.DeserializeObject<T>(result, _jsonsettings);
                }
            }
        }
        catch (Exception ex)
        {
           
        }

        return Response;
    }

Assuming you want to download the file after retrieving it, try the following in your controller action method:假设您想在检索文件后下载文件,请在您的 controller 操作方法中尝试以下操作:

var fileStream = new MemoryStream();

using (var file = await _httpClient.GetStreamAsync(request).ConfigureAwait(false))
{
      await file.CopyToAsync(fileStream);
}

fileStream.Position = 0;  
return File(fileStream , "application/pdf", "filename.pdf");  

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

相关问题 如何将 HttpResponseMessage 内容读取为文本 - How to read HttpResponseMessage content as text 如何从另一个HttpResponseMessage返回PDF内容给浏览器? - How to return PDF content from another HttpResponseMessage to the browser? 如何从HttpResponseMessage读取MultipartContent? - How to read MultipartContent from HttpResponseMessage? 如何从 HttpResponseMessage 中读取 cookie? - How to read cookies from HttpResponseMessage? Content-Type 字符集是否未从 HttpResponseMessage 公开? - Is the Content-Type charset not exposed from HttpResponseMessage? 读取和编辑HttpResponseMessage的内容 - Read and edit the content of an HttpResponseMessage 没有MediaTypeFormatter可用于从媒体类型为“ application / octet-stream”的内容中读取“ StreamContent”类型的对象 - No MediaTypeFormatter is available to read an object of type 'StreamContent' from content with media type 'application/octet-stream' 如何从HttpResponseMessage反序列化protobuf内容 - How to deserialize protobuf content from HttpResponseMessage 如何从httpResponseMessage读取zipfile作为StreamContent? - How to read zipfile as StreamContent from httpResponseMessage? 在内容 100% 完成之前从 HttpResponseMessage 读取标头 - Read headers from HttpResponseMessage before Content is 100% complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM