“Microsoft Edge PDF inline issue” Same Issue Again

余生颓废 提交于 2019-12-03 07:08:56

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.

Ansar

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;
    }
}

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

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.

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