"Microsoft Edge PDF inline issue" Same Issue Again

loowool picture loowool · Aug 6, 2015 · Viewed 9.5k times · Source

I'm still having the same issue that was previously reported and answered under Microsoft Edge PDF inline issue even though I'm not using the pre-release version of Win 10, but the latest downloaded though Windows Update.

After upgrading my Win 8.1 Machine to Win 10, and tested my ASP.NET application, I faced an issue with displaying inline pdf files.

Here's my C# code in my ASP.NET application:

Response.Clear();
Response.ClearHeaders(); 
Response.ClearContent();
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition","inline;filename=some.pdf");
Response.BinaryWrite(pdfArray); 
Response.End();

The above works on all browsers, except on Edge where it gives me the following error:

Couldn’t open PDF Something’s keeping this PDF from opening.

What am I doing wrong?

Answer

Dark Helmet picture Dark Helmet · Aug 14, 2015

Copied from my workaround on Microsoft Connect.

WARNING: This is a complete hack and runs the risk of breaking if/when Microsoft ever fixes this issue.

You'll see that Edge issues two requests whenever you view a PDF. To me, it looks like the browser is sending the initial request and then the PDF viewer is issuing its own request when it is opened. If you look at the headers in that second request, you'll see an odd DLNA header coming down, which should just be for media streaming, but that leads me to my workaround...

  1. When the request is received in your handler or page, check if the user agent string contains "Edge/12." If it doesn't, send your PDF back normally. If it does, move on to step #2.

  2. Check if the HTTP Header "GetContentFeatures.DLNA.ORG" exists. If it doesn't, that means that the request came from the browser. Just send back a Content-Type header of "application/pdf" and an empty body. If the header exists, then the request came from the PDF viewer and you can send your PDF back normally.

Basically, the handler treats that first request as a HEAD request and then responds with the full PDF if we determine that the request is coming from the PDF viewer. The risk we run here is if Microsoft removes that DLNA header later on, Edge will not render the PDF properly. Hopefully, Microsoft will fix this issue in their browser and this workaround will not be necessary.