javascript URL.createObjectURL(blob):如何为动态生成的 .pdf 提供“有意义的文件名”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50573429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:10:46  来源:igfitidea点击:

URL.createObjectURL(blob): How do I give a "meaningful filename" to a dynamically generated .pdf?

javascripthtmliframe

提问by paulsm4

I'm calling a web service to generate a .pdf, then using createObjectURL and and iframe to print and display it:

我正在调用一个 web 服务来生成一个 .pdf,然后使用 createObjectURL 和 iframe 来打印和显示它:

    var title = "Claim-" + this.claimNumber + "-" + new Date() + ".pdf";
    var blob = new Blob([wsRequest.response], { type: 'application/pdf' });
    blob.name = title;
    if (browser() === 'IE') {
        window.navigator.msSaveOrOpenBlob(blob, title);
    } else {
        var fileURL = URL.createObjectURL(blob);
        var win = window.open();
        win.document.write('<iframe name="' + title + '" src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
        win.document.title = title;

For IE, it works great: the .pdf comes up in Acrobat Reader, it displays, I can print it ... and it has a "meaningful filename".

对于 IE,它工作得很好:.pdf 出现在 Acrobat Reader 中,它显示,我可以打印它......它有一个“有意义的文件名”。

For Chrome/embedded .pdf viewer, it also works OK: it comes up in it's own tab, and the tab has "a meaningful filename".

对于 Chrome/嵌入式 .pdf 查看器,它也可以正常工作:它出现在它自己的选项卡中,并且该选项卡具有“有意义的文件名”。

If Chrome brings up the image in Acrobat reader, however:

但是,如果 Chrome 在 Acrobat 阅读器中显示图像:

a) I get a new, blank tab (with the "meaningful name")

a) 我得到一个新的空白标签(带有“有意义的名字”)

b) Acrobat displays a GUID - the GUID assigned by createObjectURL():

b) Acrobat 显示一个 GUID - createObjectURL() 分配的 GUID:

EXAMPLE: "blob:http://192.168.116.170:9080/dd554e89-0174-4b9a-bbd1-0934239a4c9"

示例:“blob:http: //192.168.116.170:9080/ dd554e89-0174-4b9a-bbd1-0934239a4c9

As you can see, neither blob.name = titleor <iframe name=" + title + "...>seem to help.

如您所见,两者都没有blob.name = title<iframe name=" + title + "...>似乎没有帮助。

Q: Is there any way I can "assign a meaningful name" to a dynamically generated .pdf if Chrome opens it in an external viewer (like Acrobat)?

问:如果 Chrome 在外部查看器(如 Acrobat)中打开动态生成的 .pdf,有什么办法可以“指定一个有意义的名称”?

采纳答案by Chris C.

One way is to save the file with a filename before it's opened. Unfortunately, this may not automatically open the file.

一种方法是在打开文件之前使用文件名保存文件。不幸的是,这可能不会自动打开文件。

var fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.download = title;
fileLink.click();

Another way is to generate the PDF and filename on your web server, and offer the link remotely, rather than generate the filename locally in the browser. This might offer you more consistent timestamps because they are generated by your server rather than all the clients in different timezones. Then you and your customers will be able to logically refer to identical documents if they have any questions.

另一种方法是在您的 Web 服务器上生成 PDF 和文件名,并远程提供链接,而不是在浏览器中本地生成文件名。这可能会为您提供更一致的时间戳,因为它们是由您的服务器生成的,而不是由不同时区的所有客户端生成的。如果他们有任何问题,那么您和您的客户将能够在逻辑上参考相同的文档。