HttpResponseMessage returning ByteArrayContent result - Preview document in chrome

情到浓时终转凉″ 提交于 2019-12-11 05:35:32

问题


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)
        {
          //..
        }
    }

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
...

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.

Any help would be much appreciated.


回答1:


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).

If trying in Mozilla

  1. Click the menu button and choose Options.
  2. Select the Applications panel.
  3. Find Portable Document Format (PDF) in the list and click on it to select it.
  4. Click on the drop-down arrow in the Action column for the above entry and select 'preview in firefox'.

Reference - https://support.mozilla.org/en-US/kb/disable-built-pdf-viewer-and-use-another-viewer

For Chrome

  1. Go to "Options" (click the wrench button on upper right)
  2. Go to: Under the Hood -> Content Settings -> Plug-ins
  3. Click on "Manage Individual Plug-ins...
  4. Locate the Adobe Acrobat plugin (or any other PDF viewer) and make sure it is enabled

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

Hope this helps



来源:https://stackoverflow.com/questions/37906586/httpresponsemessage-returning-bytearraycontent-result-preview-document-in-chro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!