<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
当你想直接从浏览器下载任何图片或 pdf 文件,而不是打开它在新标签,然后在 javascript 你应该设置值下载属性的创建动态链接
var path= "your file path will be here";
var save = document.createElement('a');
save.href = filePath;
save.download = "Your file name here";
save.target = '_blank';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
对于新的 Chrome 更新,某些时间事件不起作用。
下面的代码将被使用
var path= "your file path will be here";
var save = document.createElement('a');
save.href = filePath;
save.download = "Your file name here";
save.target = '_blank';
document.body.appendChild(save);
save.click();
document.body.removeChild(save);
添加和删除子 Internet Explorer 对于只支持浏览器的 Firefox 非常有用。在铬,它将工作没有附加和删除子