最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Download pdf without opening in browser - Stack Overflow

programmeradmin0浏览0评论

I want to download pdf file directly without viewing, i have tries following things till now but nothing is helping.

1- window.open(";, 'Download');

2- <a href="; download>Link</a>

Link -

Assume my domain is

I somewhere read we can't download cross-origin files. content-disposition header is required to be passed from backend. I am puzzled here. Another csv file of cross-origin is being downloaded easily.

I just need to download pdf file using javascript. Please guide me.

I want to download pdf file directly without viewing, i have tries following things till now but nothing is helping.

1- window.open("https://s3-link1.pdf", 'Download');

2- <a href="https://s3-link1.pdf" download>Link</a>

Link - https://s3-link1.pdf

Assume my domain is https://www.somedomain.

I somewhere read we can't download cross-origin files. content-disposition header is required to be passed from backend. I am puzzled here. Another csv file of cross-origin is being downloaded easily.

https://s3-link2.csv

I just need to download pdf file using javascript. Please guide me.

Share Improve this question edited Sep 3, 2020 at 13:32 Pooja Verma asked Sep 3, 2020 at 11:55 Pooja VermaPooja Verma 1431 gold badge4 silver badges14 bronze badges 3
  • Not sure about the context. You can right click the link and "Save as..." from context menu. – hackape Commented Sep 3, 2020 at 12:02
  • Do you want a script that can copy-paste into browser console and batch download links from the page you're currently viewing? – hackape Commented Sep 3, 2020 at 12:03
  • I think what she means as long as it hits on url. File should automatically downloaded – Shubham Verma Commented Sep 3, 2020 at 12:07
Add a ment  | 

2 Answers 2

Reset to default 5

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:

$.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.

In the latest versions of Chrome, you cannot download cross-origin files (they have to be hosted on the same domain).

<a href="https://s3-link1.pdf" download>Download PDF</a>

Find more about it here on this blog: https://actualwizard./html-force-download-file

发布评论

评论列表(0)

  1. 暂无评论