[英]Download pdf without opening in browser
I want to download pdf file directly without viewing, i have tries following things till now but nothing is helping.我想直接下载 pdf 文件而不查看,到目前为止我已经尝试了以下内容,但没有任何帮助。
1- window.open("https://s3-link1.pdf", 'Download');
2- <a href="https://s3-link1.pdf" download>Link</a>
Link - https://s3-link1.pdf链接 - https://s3-link1.pdf
Assume my domain is https://www.somedomain.com假设我的域是https://www.somedomain.com
I somewhere read we can't download cross-origin files.我在某处读到我们无法下载跨域文件。 content-disposition header is required to be passed from backend.
需要从后端传递 content-disposition 标头。 I am puzzled here.
我在这里很困惑。 Another csv file of cross-origin is being downloaded easily.
另一个跨域 csv 文件正在轻松下载。
https://s3-link2.csv https://s3-link2.csv
I just need to download pdf file using javascript.我只需要使用 javascript 下载 pdf 文件。 Please guide me.
请指导我。
Try with fetch.尝试获取。
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);
})
Option 1: with jQuery you can try this:选项 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();
});
Option 2: The download
attribute works in all modern browsers, including MS Edge, but not Internet Explorer.选项 2:
download
属性适用于所有现代浏览器,包括 MS Edge,但不适用于 Internet Explorer。
In the latest versions of Chrome, you cannot download cross-origin files (they have to be hosted on the same domain).在最新版本的 Chrome 中,您无法下载跨域文件(它们必须托管在同一域中)。
<a href="https://s3-link1.pdf" download>Download PDF</a>
Find more about it here on this blog: https://actualwizard.com/html-force-download-file在此博客上找到更多相关信息: https : //actualwizard.com/html-force-download-file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.