简体   繁体   English

无法从mac上的safari 12.1保存blob url pdf

[英]Can't save blob url pdf from safari 12.1 on mac

I am setting the iframe src to a blob url with format "blob: http://localhost:3000/f87808a3-9a74-4d61-b7ae-7ac37ff38325 ". 我将iframe src设置为blob url,格式为“blob: http:// localhost:3000 / f87808a3-9a74-4d61-b7ae-7ac37ff38325 ”。 The iframe displays a pdf which was created with some javascript. iframe显示使用一些javascript创建的pdf。 Displaying the pdf works as expected in all browsers. 显示pdf在所有浏览器中都按预期工作。 In Chrome and Firefox it is possible to download this pdf to the hard drive, with the browsers integrated pdf viewers. 在Chrome和Firefox中,可以将此pdf下载到硬盘驱动器,浏览器集成了pdf查看器。

However in Safari 12.1 on mac, when clicking the download button of the pdf viewer nothing happens. 但是在Mac上的Safari 12.1中,单击pdf viewer的下载按钮时没有任何反应。

Is this a known bug in Safari? 这是Safari中的已知错误吗?

Are there ways to make pdf blob url downloads work in Safari? 有没有办法让Safari的blob网址下载工作?

This is related to my answer here . 这关系到我的答案在这里

Apparently this is a Safari 12.1 bug that sometimes happens. 显然这是一个有时会发生的Safari 12.1错误 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: 在修复bug之前,丑陋的解决方法是:

  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: 在download.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