-->

“Microsoft Edge PDF inline issue” Same Issue Again

2020-06-03 04:00发布

问题:

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?

回答1:

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.



回答2:

Thanks Dark Helmet, you saved my day. I implemented the solution in java. Here is the code that might help others.

String userAgent = request.getHeader("user-agent");
System.out.println(userAgent);
if(userAgent.contains("Edge")){
    String dlnaHeader = request.getHeader("getcontentfeatures.dlna.org");
    System.out.println(dlnaHeader);
    if(dlnaHeader == null ){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] result = baos.toByteArray();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition","inline; ");
        response.setContentLength(result.length);               
        ServletOutputStream sos = response.getOutputStream();
        sos.write(result);
        return null;
    }
}


回答3:

Thanks guys, I just want to put my VB.NET solution here based on your workaround.

Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.Buffer = True
If Request.Headers.Item("User-Agent").Contains("Edge") _
AndAlso IsNothing(Request.Headers.Item("GetContentFeatures.DLNA.ORG")) Then
    'Edge? Send empty output if special header not exist
    Response.ContentType = "application/pdf"
    Dim bTemp As Byte()
    Response.BinaryWrite(bTemp) 'Empty output
    Response.Flush()
    Response.SuppressContent = True
    HttpContext.Current.ApplicationInstance.CompleteRequest()
End If
'Normal process:
Response.ContentType = "application/pdf"
Response.BinaryWrite(pdfArray)
Response.Flush()
Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()


回答4:

With Edge 16.16299 (Windows Fall Creator Update) there were made changes here. We did the workaround described in this issue and it worked "well". But now with the new version of Edge (16.16299) it isn’t working anymore and it happens, that the PDFs are corrupted (0 bytes large). Take care if you implemented this workaround somewhere. What you also take care of is that Edge is doing two requests like before.