简体   繁体   English

Safari 12 不会下载 PDF blob

[英]Safari 12 Won't Download a PDF blob

This code is used to download a pdf via blob.此代码用于通过 blob 下载 pdf。 It works fine on every browser except Safari 12 for macOS and iOS.它适用于除 macOS 和 iOS 的 Safari 12 之外的所有浏览器。 Even Safari 11 works.甚至 Safari 11 也能工作。 When I run the code the very first time, it works fine, but every time after that it gives me "WebKitBlobResource error 1"当我第一次运行代码时,它工作正常,但之后每次都会给我“WebKitBlobResource 错误 1”

 function downloadFileFromBlob(fileBlob, fileName) { if (/\\bMSIE\\b|\\bTrident\\b/.test($window.navigator.userAgent)) { $window.navigator.msSaveOrOpenBlob(fileBlob, fileName); } else { var fileURL = $window.URL.createObjectURL(fileBlob); createDownloadElementAndClick(fileURL, fileName); } } function createDownloadElementAndClick(fileURL, fileName) { var anchorElement = $window.document.createElement('a'); anchorElement.href = fileURL; anchorElement.target = '_blank'; anchorElement.download = fileName; var event = $window.document.createEvent("MouseEvents"); event.initEvent("click", true, false); anchorElement.dispatchEvent(event); }

It seems that it is the target = "_blank" that is not working.似乎是target = "_blank"不起作用。 I have replaced it with _self , which apparently solved the problem.我已经用_self替换了它,这显然解决了这个问题。 I found this when I had the same issue.当我遇到同样的问题时,我发现了这一点。

If someone has a idea on why we cannot use _blank I would love to hear that.如果有人知道为什么我们不能使用_blank我很乐意听到。

Apparently this is a Safari 12 bug that sometimes happens.显然,这是有时会发生的Safari 12 错误 It's not fixed by target = "_self" , which pertains to a different regression bug .它不是由target = "_self"修复的,它与不同的回归错误有关

Until the bug is fixed, the ugly workaround is:在错误修复之前,丑陋的解决方法是:

  1. Send the blob to the server which saves the file remotely.将 blob 发送到远程保存文件的服务器。
  2. Download the remote file.下载远程文件。

Javascript Code Javascript代码

   async createDownloadElementAndClick(blob, fileName) {
            let options = {
                method:"POST",
                body:blob
            };

            await fetch(`https://example.com/upload.php`, options);

            window.open(`https://example.com/download.php?${fileName}`, "_self");
    }

PHP Code PHP代码

In upload.php:在upload.php中:

<?php    
// add any authentication code as necessary here


    // gets entire POST body
    $data = file_get_contents('php://input');

    $filename = "temp/download.pdf";
    // write the data out to the file
    $fp = fopen($filename, 'wb');

    fwrite($fp, $data);
    fclose($fp);
?>

In download.php:在下载.php中:

<?php
    ob_start();
    $file = $_SERVER["QUERY_STRING"];

    // This is the line that tells Safari to download the file instead of opening it
    header("Content-disposition: attachment; filename=$file");
    header("Content-type: application/pdf", false);
    readfile("temp/download.pdf");

    ob_flush();
    // This deletes the pdf so there is little chance of contaminating the next call
    unlink("temp/download.pdf");
?>

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

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