How to set file name for base64 pdf in iframe?

I have a PDF report in base64, I want to show a file embedded in an HTML page. For this, I use iFrame and it works, but the problem is that I can not set the PDF name so that when it is loaded it will be saved with the assigned name.

Can I set the file name ?, if so, how can I do this?

this is my code:

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

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

I tried different ways, but not working:

  • Adding download attribute = "filename.pdf"

     var iFrameBlock = $("<iframe>").attr({ "src": base64file_encode, "width": "100%", "height": "100%", "frameborder": "0", "download": "filename.pdf" }); 
  • I tried to change the value inside the iframe, but I was able to find the correct attribute, and Chrome rejected 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 to prevent the use of any other library.

thank you for your time

+5
source share
1 answer

An iframe cannot do this, but as an alternative, you can add an 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. A.

0
source

Source: https://habr.com/ru/post/1259506/


All Articles