自动弹出另存为Firefox的窗口

时间:2013-09-02 03:36:24

标签: html5 popup

我一直在使用此代码<a href="FILEPATH">download PDF</a>直接下载pdf链接,但此代码仅适用于Google Chrome

有人可以帮助我在Mozilla Firefox和IE8中完成这项工作。因为当我尝试在Mozilla Firefox中测试它时,它会打开链接,但不会弹出“另存为”窗口。

2 个答案:

答案 0 :(得分:0)

我会使用PHP标头,因此请使用以下内容为url引用该文件:

 <a href="/download.php?file=myFile.pdf">download PDF</a>

然后使用php来传递文件:

 <?php
 /* put some validation and injection protection here */
 $approvedFiles = ["myFile.pdf","myOtherFile.pdf"];

 if (!in_array($_GET['file'],$approvedFiles)){
  header("HTTP/1.1 404 Not Found"); 
  die();
 }

 //otherwise
 header('Content-type: application/pdf');
 header('Content-Disposition: attachment; filename="myFile.pdf"');
 readfile('myFile.pdf'); 

注意:伪代码并不完美:)

参考:http://php.net/manual/en/function.header.php

答案 1 :(得分:0)

http://jsfiddle.net/RemMp/

<a id="save_data" href="">link</a>

window.URL = window.webkitURL || window.URL;
var a = document.getElementById('save_data');

var data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==';
var filename = 'qqq.pdf';

a.download = filename;
a.href = data;
a.textContent = 'Downloading...';

//release memory
a.onclick = function(e){
    save_cleanup(this);
    };
//force click
document.querySelector('#save_data').click();

function save_cleanup(a){
    a.textContent = 'Downloaded';
    setTimeout(function(){
        a.href = '';
        var element = document.getElementById("save_data");
        element.parentNode.removeChild(element);
        }, 1500);
    };
相关问题