简体   繁体   English

FileSaver.js 不使用 Safari 下载 PDF

[英]FileSaver.js doesn't download PDF with Safari

I have an issue with FileSaver.js , I can not download a PDF (or PNG or excel file) on Safari, but it works on any other web browser.我对FileSaver.js有疑问,我无法在 Safari 上下载 PDF(或 PNG 或 excel 文件),但它适用于任何其他网络浏览器。 I get the error in the console : 'Failed to load resource: The network connection was lost.'我在控制台中收到错误:“加载资源失败:网络连接丢失。”

What is weird is that, this PDF file doesn't get downloaded if Tomcat serves it, but if it is Apache that serves the file, the download works fine.奇怪的是,如果 Tomcat 提供此 PDF 文件,则不会下载它,但如果是 Apache 提供该文件,则下载工作正常。

Here is a sample of code (I am working with angular 1.5.8):这是一个代码示例(我正在使用 angular 1.5.8):

$http.get(url, { responseType: 'arraybuffer' })
            .success(function (response) {
                var file = new Blob([response], {type: 'application/pdf'});
                fileSaverService(file, filename);
            });

I had a similar issue and I was using axios to make a call (it was post request in my case) to the download service. 我有一个类似的问题,我正在使用axios拨打电话(在我的情况下是发布请求)到下载服务。 The code below worked for me: 以下代码对我有用:

axios.post(url, downloadRequest, {responseType:'blob'})
    .then(response =>{              
        var filename = 'example.zip';
        var blob = new Blob([response.data], {type:"application/octet-stream"});
        saveAs(blob , filename);
    })
    .catch(error => {
        console.error(error);       
    });

I had a issue with base64,Pdf was not able to open.我遇到了 base64 的问题,Pdf 无法打开。 This i have resolved converting base64 to bin and than converted into Uint8Array(byteNumbers) .我已经解决了将 base64 转换为 bin 并转换为Uint8Array(byteNumbers)的问题。 Check below snippet which has worked for me.检查下面对我有用的片段。

const byteCharacters = atob(b64);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
   byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
            
var blob = new Blob([byteArray], {type: "application/pdf"});
saveAs(blob, "sample.pdf");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM