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.
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
Reference - https://support.mozilla.org/en-US/kb/disable-built-pdf-viewer-and-use-another-viewer
For Chrome
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
Hope this helps