HttpResponseMessage 内容不会显示 PDF

Posted

技术标签:

【中文标题】HttpResponseMessage 内容不会显示 PDF【英文标题】:HttpResponseMessage Content won't display PDF 【发布时间】:2014-05-02 08:32:13 【问题描述】:

我创建了一个 Web Api,它返回一个 HttpResponseMessage,其中的内容设置为 PDF 文件。如果我直接调用 Web Api 效果很好,并且 PDF 会在浏览器中呈现。

response.Content = new StreamContent(new FileStream(pdfLocation, FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Headers.ConnectionClose = true;
        return response;

我有一个 MVC 客户端想联系 Web Api,请求 Pdf 文件,然后以与上述相同的方式将其呈现给用户。

不幸的是,我不确定问题出在哪里,但即使我设置了内容类型:

response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

当我点击调用 web api 的链接时,我得到了 HttpResponseMessage 的文本渲染。

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:  Connection: close Content-Disposition: attachment Content-Type: application/pdf 

我认为客户端应用程序缺少一些设置,使其能够像我的 Web Api 一样呈现 PDF...

任何帮助将不胜感激。 谢谢

【问题讨论】:

【参考方案1】:

经过数小时的 Google 搜索以及反复试验,我已经解决了这个问题。

我没有将响应的内容设置为 StreamContent,而是在 Web Api 端将其更改为 ByteArrayContent。

byte[] fileBytes = System.IO.File.ReadAllBytes(pdfLocation);
response.Content = new ByteArrayContent(fileBytes);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

通过这种方式,我的 MVC 4 应用程序能够使用 WebClient 和 DownloadData 方法下载 PDF。

internal byte[] DownloadFile(string requestUrl)

    string serverUrl = _baseAddress + requestUrl;
    var client = new System.Net.WebClient();
    client.Headers.Add("Content-Type", "application/pdf");
    return client.DownloadData(serverUrl);

返回的 Byte[] 数组可以在返回 File 输出之前轻松转换为 MemoryStream...

Response.AddHeader("Content-Disposition", "inline; filename="+fileName);
MemoryStream outputStream = new MemoryStream();
outputStream.Write(file, 0, file.Length);
outputStream.Position = 0;
return File(outputStream, "application/pdf");

我希望这对其他人有用,因为我已经浪费了很多时间来让它发挥作用。

【讨论】:

我还将一个字节数组从 web api 返回到客户端。我想在客户端上将该字节数组下载为 pdf。您如何处理在输出流中获得的客户端响应? 上面的第二和第三组代码显示了它。我在上面创建了一个 Web 客户端,并将 Web Api 的 url 传递给它。 Web 客户端从 Web Api 获取 Byte[] 并转换为 MemoryStream(这样我就不会在本地保存文件)并作为 PDF 文件返回给 Web 客户端(浏览器)。 我的 api 控件有 [Authorize[ 属性。我无法直接从 web api 发送文件。 ajax 请求完成后,我必须以某种方式在客户端处理 byte[]。 data = "data:application/octet-stream;base64," + data; document.location = data; 这些行确实下载了文件,但文件没有任何名称或扩展名。我只是看到一个“下载”作为文件的名称。如何在客户端设置文件名? 这是我命名文件的方式,但我在服务器上这样做... Response.AddHeader("Content-Disposition", "inline; filename="+ fileName);【参考方案2】:

只需返回 PhysicalFileResult 并使用 HttpGet 方法,url 将打开 pdf 文件

public ActionResult GetPublicLink()




            path = @"D:\Read\x.pdf";
            return new PhysicalFileResult(path, "application/pdf");

【讨论】:

以上是关于HttpResponseMessage 内容不会显示 PDF的主要内容,如果未能解决你的问题,请参考以下文章

从 HttpResponseMessage 获取内容/消息

将内容放在 HttpResponseMessage 对象中?

如何从 javascript 中的 C# HttpResponseMessage 读取内容?

在内容 100% 完成之前从 HttpResponseMessage 读取标头

WCF Restful返回HttpResponseMessage想要在设置内容时进行协商

无法使任务 <HttpResponseMessage> “等待”