Set default filename when downloading a PDF open w

2019-08-09 02:54发布

问题:

My angular app has an iframe that renders a PDF:

<iframe ng-src="{{PCtrl.docSrc}}" type="application/pdf" ...></iframe>

docSrc is generated as a BASE64 encoded string, something like:

"data:application/pdf;base64,JVBERi0xLjMKMyA..."

Chrome renders the embedded PDF just fine. The PDF can be downloaded clicking on download, and the user will be presented with a "save as" dialog. The default filename given by the Chrome PDF viewer is "download.pdf", how do I change it?

回答1:

window.onload = () => {
  let blob = new Blob(["file"], {
    type: "text/plain"
  });
  let url = URL.createObjectURL(blob);
  let a = document.createElement("a");
  a.href = url;
  a.download = "file.txt";
  document.body.appendChild(a);
  console.log(url);
  a.click();
}

may be this will help you



回答2:

Similar suggestions provided by Silvia also in #28310366 which also includes a reference to https://github.com/alferov/angular-file-saver which itself leverages https://github.com/eligrey/FileSaver.js/