Javascript 如何在后台下载PDF文件

Javascript 如何在后台下载PDF文件,javascript,jquery,Javascript,Jquery,如何在不离开当前页面的情况下在移动浏览器的后台下载文件 我看了这篇文章: 使用以下代码在同一窗口中显示文件(本例中为pdf): var file_path = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'; var a = document.createElement('A'); a.href = file_path; a.download = file_path.substr(file_pat

如何在不离开当前页面的情况下在移动浏览器的后台下载文件

我看了这篇文章:

使用以下代码在同一窗口中显示文件(本例中为pdf):

var file_path = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
var a = document.createElement('A');
a.href = file_path;
a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
如果您希望在当前窗口中立即显示PDF,则此操作非常有效

但是,如何保持当前视图并让文件下载/显示显示正在下载的对话框


目标是不要打开新的选项卡或窗口来让用户参与当前页面。我在网上浏览了S/O&但没有找到解决方案。感谢您提供此问题的任何解决方案。

您可以使用HTML web worker


您可以使用HTML web worker


谢谢你的回答。很抱歉,如果这看起来像是一个n00b问题(我不熟悉使用JS),那么我应该在
pdf\u workers.JS
文件中做什么呢?事实上,pdf\u workers.JS只是运行代码的文件名。这个过程是:1)创建一个新的web worker对象2)运行“demo_workers.js”中的代码谢谢你的回答。很抱歉,如果这看起来像是一个n00b问题(我不熟悉使用JS),那么我应该在
pdf\u workers.JS
文件中做什么呢?事实上,pdf\u workers.JS只是运行代码的文件名。这个过程是:1)创建一个新的web worker对象2)运行“demo_workers.js”中的代码
var w;

function stopWorker() {
  w.terminate();
  w = undefined;
}

function downloadPDFBackground() {
  if (typeof(Worker) !== "undefined") {
    if (typeof(w) == "undefined") {
      w = new Worker("pdf_workers.js");
    }
    w.onmessage = function(event) {
        var file_path = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
        var a = document.createElement('A');
        a.href = file_path;
        a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        stopWorker();
    };
  } else {
    document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
  }
}