简体   繁体   English

如何使用javascript从网址下载pdf?

[英]How can I download a pdf from a url using javascript?

I need to download pdf's from one of our online resources.There is no built in function to batch download.The only way to do it is to navigate to each pdf file, click to open, then click download. 我需要从我们的一个在线资源下载pdf。批量下载没有内置功能。唯一的方法是导航到每个pdf文件,点击打开,然后点击下载。 There are several thousand files and this would take a very long time to do. 有几千个文件,这需要很长时间。 I got around this in the past using javascript. 我过去使用javascript解决了这个问题。 I gathered all the links to the pdfs, put them in a csv, and had the code loop through each link, download, and move onto the next link. 我收集了pdf的所有链接,将它们放在csv中,并让代码循环遍历每个链接,下载并转到下一个链接。

Unfortunately, I have lost that code and my efforts to recreate it have been unsuccessful. 不幸的是,我丢失了这些代码,我重新创建它的努力却没有成功。

I have tried everything in this article: How to download PDF automatically using js? 我已经尝试了本文中的所有内容: 如何使用js自动下载PDF?

I have tried the code from this article (which I'm pretty sure is what I did before): https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/ 我已经尝试过本文中的代码(我很确定我以前做过的): https//www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-使用-JS /

This is what I think should work...per the second article I referenced above 根据我上面引用的第二篇文章,这是我认为应该有效的

function download_file(fileURL, fileName) {
var link = document.createElement('a');
link.href = fileURL;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
}
var fileURL = "link/to/pdf";
var fileName = "test.pdf";
download(fileURL,fileName);

The code above is just to test download one file from a hardcoded URL. 上面的代码只是测试从硬编码的URL下载一个文件。 If it worked as intended, when the page is loaded, it should download the pdf from the provided url. 如果它按预期工作,当页面加载时,它应该从提供的URL下载pdf。 Instead, it doesn't do anything on load or refresh. 相反,它不会在加载或刷新时执行任何操作。 Any suggestions? 有什么建议么?

Please check https://stackoverflow.com/a/18983688/6923146 请检查https://stackoverflow.com/a/18983688/6923146

<a href="http://www.africau.edu/images/default/sample.pdf" download="sample.PDF">click me</a>

Another one https://stackoverflow.com/a/45905238/6923146 另一个https://stackoverflow.com/a/45905238/6923146

function download(url, filename) {
fetch(url).then(function(t) {
    return t.blob().then((b)=>{
        var a = document.createElement("a");
        a.href = URL.createObjectURL(b);
        a.setAttribute("download", filename);
        a.click();
    }
    );
});
}

download("https://get.geojs.io/v1/ip/geo.json","geoip.json")
download("data:text/html,Hello Developer!", "HelloDeveloper.txt");

I hope it helpfull 我希望它有用

https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/ https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/

  1. You must add link element to DOM 您必须将链接元素添加到DOM

 function download_file(fileURL, fileName) { var link = document.createElement('a'); link.href = fileURL; link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); } var fileURL = "https://cdn.sstatic.net/clc/img/jobs/bg-remote-header-sm.png"; var fileName = "test.pdf"; download_file(fileURL, fileName); // fix function name 

  1. Link must be in same origin 链接必须在同一个来源

    The download attribute on anchor was ignored because its href URL has a different security origin. 锚点上的download属性被忽略,因为它的href URL具有不同的安全性来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM