[英]Download pdf without opening in browser
我想直接下載 pdf 文件而不查看,到目前為止我已經嘗試了以下內容,但沒有任何幫助。
1- window.open("https://s3-link1.pdf", 'Download');
2- <a href="https://s3-link1.pdf" download>Link</a>
鏈接 - https://s3-link1.pdf
假設我的域是https://www.somedomain.com
我在某處讀到我們無法下載跨域文件。 需要從后端傳遞 content-disposition 標頭。 我在這里很困惑。 另一個跨域 csv 文件正在輕松下載。
我只需要使用 javascript 下載 pdf 文件。 請指導我。
嘗試獲取。
fetch("https://s3-link1.pdf", {
method: 'GET'
}).then(resp => resp.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = "name"; // the filename you want
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
選項 1:使用 jQuery 你可以試試這個:
$.get("https://s3-link1.pdf", function (result)
{
var blob = new Blob([result]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "myFileName.pdf";
link.click();
});
選項 2: download
屬性適用於所有現代瀏覽器,包括 MS Edge,但不適用於 Internet Explorer。
在最新版本的 Chrome 中,您無法下載跨域文件(它們必須托管在同一域中)。
<a href="https://s3-link1.pdf" download>Download PDF</a>
在此博客上找到更多相關信息: https : //actualwizard.com/html-force-download-file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.