Chrome sends two requests when downloading a PDF (and cancels one of them)

前端 未结 3 1964
梦谈多话
梦谈多话 2020-12-14 08:25

I noticed that whenever you download a PDF in Chrome, it consistently makes two requests, and then cancels one of them. This is causing the request to be registered twice in

相关标签:
3条回答
  • 2020-12-14 08:36

    I'm still trying to find a proper solution but as a partial "fix" for now you could have two options

    1) set the content disposition to "attachment" in the header

    setting that to "inline" cause chrome to run a second cancelled call

    so for example you can do something like that (nodejs resp in example)

    res.writeHead(200, {
        'Content-Type' : 'application/pdf',
        'Access-Control-Allow-Origin' : '*',
        'Content-Disposition' : 'attachment; filename=print.pdf'
    });
    

    unfortunately this solution will force the browser to download the pdf straight away instead of rendering it inline and that's not maybe desiderable

    2) adding "expires" in the headers this solution will always fire a second cancelled call but it's ignored by the server

    so for example you can do something like that (nodejs resp in example)

    res.writeHead(200, {
        'Content-Type' : 'application/pdf',
        'Access-Control-Allow-Origin' : '*',
        'Content-Disposition' : 'inline; filename=print.pdf',
        'Expires' : new Date(new Date().getTime() + (60000))
    });
    
    0 讨论(0)
  • 2020-12-14 08:48

    It looks like a bug in Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=587709

    The problem is that Chrome, when it loads an iframe that returns a PDF stream, writes an "embed" tag inside that iframe which again contains the same URL as the iframe. This triggers a request for that URL again, but Chrome immediately cancels it. (see the network tab) But by that time, the damage is done.

    We have the same issue here, and it does not occur in Firefox or IE.

    We're still looking for a good solution to this problem.

    0 讨论(0)
  • 2020-12-14 08:54

    I had the same problem in an iframe. I turned of the PDF Viewer extension and the problem disappeared. I'm thinking the extension downloads the file twice. The first time to get the size, the second time to download with a progress bar (using the size gathered in the first request)

    0 讨论(0)
提交回复
热议问题