HttpResponseMessage返回ByteArrayContent结果-chrome中的预览文档 [英] HttpResponseMessage returning ByteArrayContent result - Preview document in chrome

查看:101
本文介绍了HttpResponseMessage返回ByteArrayContent结果-chrome中的预览文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Chrome中预览文件,但它一直在下载.

I am trying to preview a file in Chrome but it keeps downloading.

    [HttpGet]
    [ResponseType(typeof(ByteArrayContent))]
    public HttpResponseMessage Download([FromUri] int uploadId)
    {
        try
        {
            Upload upload = UploadController.LoadByPrimaryKey(uploadId);

            var path = upload.FullPath + "\\" + upload.UploadGuid + upload.Extension;
            var mimeType = MimeTypeMap.GetMimeType(upload.Extension);

            MemoryStream pdf = new MemoryStream(File.ReadAllBytes(path));
            HttpResponseMessage result = null;
            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(pdf.ToArray());
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
            result.Content.Headers.ContentDisposition.FileName = upload.OriginalFileName;
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);

            return result;
        }
        catch (Exception ex)
        {
          //..
        }
    }

这是Fiddler的踪迹.

This is the trace from Fiddler.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 793708
Content-Type: application/pdf
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: http://localhost:6701
Access-Control-Allow-Credentials: true
Content-Disposition: inline; filename="1451048-Customer Acceptance.pdf"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 19 Jun 2016 10:52:35 GMT
...

我看过在浏览器中打开PDF文件而不是下载pdf文件如何强制文件在浏览器中打开而不是下载(pdf)?,但是我仍然遇到麻烦.

I have seen Open PDF file in browser rather than downloading pdf file and How to force files to open in browser instead of download (pdf)? but I am still having trouble.

任何帮助将不胜感激.

推荐答案

我看不到您的代码有任何问题,它似乎与您的浏览器设置有关(尽管大多数浏览器都具有默认设置来呈现PDF)否则).

I don't see any issue with your code it looks to be more of issue with your browser settings (although most of the browser have default setting to render PDF have seen otherwise).

如果在Mozilla中尝试

If trying in Mozilla

  1. 单击菜单按钮,然后选择选项.
  2. 选择应用程序"面板.
  3. 在列表中找到可移植文档格式(PDF),然后单击以将其选中.
  4. 单击操作"列中的下拉箭头,以获取上述条目,然后选择在firefox中预览".

参考- https://support.mozilla.org/zh-CN/kb/disable-built-pdf-viewer-and-use-another-viewer

对于Chrome

  1. 转到选项"(单击右上角的扳手按钮)
  2. 转到:高级功能->内容设置->插件
  3. 点击管理单个插件...
  4. 找到Adobe Acrobat插件(或任何其他PDF查看器)并确保已启用

我的API中有完全相同的代码,它使我能够基于ContentDispositionHeaderValue(内联呈现和附件下载)呈现PDF或下载内容

I have the exact same code in my API and it allows me to render PDF or download based on the ContentDispositionHeaderValue (inline to render and attachment to download)

请参阅下面来自我的服务器的响应标头

See below response headers from my server

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 46270
Content-Type: application/pdf
Server: Microsoft-IIS/10.0
Content-Disposition: inline; filename=Sample.pdf
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 20 Jun 2016 03:38:39 GMT

希望这会有所帮助

这篇关于HttpResponseMessage返回ByteArrayContent结果-chrome中的预览文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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