Set default filename when downloading a PDF open with Chrome PDF viewer

末鹿安然 提交于 2020-01-05 07:14:45

问题


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/



来源:https://stackoverflow.com/questions/48464731/set-default-filename-when-downloading-a-pdf-open-with-chrome-pdf-viewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!