我如何在iframe中设置base64 pdf的文件名? [英] How can I set the filename for base64 pdf in iframe?

查看:2087
本文介绍了我如何在iframe中设置base64 pdf的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在base64中有一份PDF报告,我想显示嵌入在HTML页面中的文件。要做到这一点,我使用的iFrame和工作,但问题是,我不能设置PDF的名称,以便下载时将它保存与分配的名称。



这是我的代码:


$ b $可以设置文件名吗?如果是,我该怎么办? b


结果:
预览PDF



当我尝试下载文件时,要保存的名称是download.pdf
保存对话框



我尝试了不同的方法,但也无法使用:

添加属性 下载=文件.pdf / strong>

var iFrameBlock = $(< iframe>)。attr({src:base64file_encode, width:100%,height:100%,frameborder:0,download:filename.pdf}); 我试图改变iframe中的值,但我可以找到正确的属性,并且Chrome否认了这一行为。

var iframe = $(iframe)[0]; var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; //这里我想是文件名$(iframeDocument).find(viewer-pdf-toolbar)的标签;

然而,

,我想阻止使用任何其他库。



感谢您的时间

解决方案

iframe 元素不能做到这一点,但作为替代方案,您可以添加锚标签。

<$ ($ {$ b $id:iframe,
src:base64file_encode,$ b)pre> var iFrameBlock = $(< iframe> $ bwidth:100%,
height:100%,
frameborder:0
});
var aTag = $(< a> Dwonload< / a>)。attr({
href:base64file_encode,
download:yourName.pdf
});
$(#previewPDF)。html(iFrameBlock);
aTag.insertAfter($(#iframe));

问候,KJ。


I have a report PDF in base64, I want to show the file embedded in the HTML page. To do this I use the iFrame and works, but the problem is that I can't set the name of the PDF so that when downloaded it will be saved with the name assigned.

Can I set the filename?, if yes, how can I do it?

this is my code:

var iFrameBlock = $("<iframe>").attr({
      "src": base64file_encode,
      "width": "100%",
      "height": "100%",
      "frameborder": "0"
});

$("#previewPDF").html(iFrameBlock);

The result: Preview of PDF

And when I try download the file, the name to be saved is "download.pdf" Save dialog

I tried differents ways, but do not work either:

  1. Adding attribute download="filename.pdf"

       
       var iFrameBlock = $("<iframe>").attr({
          "src": base64file_encode,
          "width": "100%",
          "height": "100%",
          "frameborder": "0",
          "download": "filename.pdf"
       });
       
       

  2. I tried to change the value inside the iframe, but I could find the correct attribute and the Chrome denied the action.

       
       var iframe = $("iframe")[0];
       var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
       //here I suppose is the label of the filename
       $(iframeDocument).find("viewer-pdf-toolbar");
       
       

However, I want prevent use any other library.

Thank you for your time

解决方案

The iframe element can't do that, but as an alternative you can add anchor tag.

var iFrameBlock = $("<iframe>").attr({
    "id": "iframe",
    "src": base64file_encode,
    "width": "100%",
    "height": "100%",
    "frameborder": "0"
});
var aTag = $("<a>Dwonload</a>").attr({
    "href": base64file_encode,
    "download": "yourName.pdf"
});
$("#previewPDF").html(iFrameBlock);
aTag.insertAfter($("#iframe"));

Regards, KJ.

这篇关于我如何在iframe中设置base64 pdf的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆