Unable to download pdf blob url on Safari

后端 未结 2 1420
旧巷少年郎
旧巷少年郎 2021-01-13 08:25

Currently using FileReader to open and view a PDF that works on Chrome. However, when the pdf is opened on Safari and you click the download button, nothing happens.

2条回答
  •  有刺的猬
    2021-01-13 08:35

    After going through the same scenario as @andreasonny83 answer, plus having the headache issue of an adblock that closed the tab opened with window.open in a split second, I found a solution that worked for me. Not sure if it works in Safari since i do not have a mac to test it. Tried on Firefox and Chrome. I'm sure if it doesn't work on IE or Safari there's a workaround. What I did was using the embed html5 component providing as a src, an URL.createObject(blob) object. That way I was able to render/refresh and download the pdf.

      // pdfBlob is the pdf fetched from the API as a blob.
      pdf = URL.createObjectURL(pdfBlob)
    

    Then you just embed that pdf into an embed tag like this:

    The key was to use the html5 tag embed. It worked nicely. Of course you need to find a way to set the embed src attribute. I'm using react and redux so i use jsx, but you can use plain vanilla javascript or whatever you like.

提交回复
热议问题