HTT presponseMessage的内容将无法显示PDF [英] HttpResponseMessage Content won't display PDF

查看:208
本文介绍了HTT presponseMessage的内容将无法显示PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了返回的Htt presponseMessage在其内容被设定到PDF文件的Web API。如果我直接调用网页API它的伟大工程和PDF在浏览器中呈现。

I have created a Web Api that returns an HttpResponseMessage in which the content is set to a PDF file. If I call the Web Api directly it works great and the PDF is rendered in the browser.

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阿比,要求全文文件,那么它呈现在与上述相同的方式向用户

I have an MVC Client that would like to do contact the Web Api, request the Pdf file then render it to the user in the same way as above.

不幸的是,我不知道问题出在哪里,但即使我设置的Content-Type:

Unfortunately, I'm not sure where the problem is but even though I set the the content-type:

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

当我点击调用网页API的链接,我得到的文本渲染的Htt presponseMessage。

When I click on the link that calls the web api, I get a text rendering of the HttpResponseMessage.

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

我想客户端应用程序缺少一些设置,将允许它来显示PDF喜欢我的Web API并...

I'm thinking that the Client Application is missing some setting that will allow it to render the PDF like my Web Api does...

任何帮助将是AP preciated。
谢谢

Any help would be appreciated. Thanks

推荐答案

在谷歌的搜索时间,以及试验和错误,我已经在这里解决了问题。

After hours of Google searching as well as trial and error, I have resolved the issue here.

而不是设置应对我已经把它改成ByteArrayContent在网页API侧StreamContent的内容。

instead of setting the content of the response to a StreamContent I have changed it to ByteArrayContent on the Web Api side.

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应用程序能够使用Web客户端和DownloadData方法,请下载PDF。

By doing it this way, my MVC 4 application is able to download the PDF using a WebClient and the DownloadData method.

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);
}

的字节[]数组返回文件输出之前被返回可以很容易地转换为一个MemoryStream ...

The Byte[] array that is returned can easily be converted to a MemoryStream before returning the File for output...

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");

我希望,我也浪费了大量的时间来得到它的工作这可能是对别人有用。

I hope this can be useful to others as I have wasted a lot of time to get it working.

这篇关于HTT presponseMessage的内容将无法显示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆