BinaryToBase64 vs. Base64ToBinary
Application Type
Traditional Web
Service Studio Version
11.10.21 (Build 41597)

Hi all,

during my preparation I use the BinaryToBase64 to load a binary and put it in a "IFRAME source": it works great.

Now ... how to do the opposite action ?

I would like a button and save the content of the IFRAME, creating the binary file using the Base64ToBinary.

I need an help ... if it is possible ...


Thanks !

mvp_badge
MVP

Hi Luca,

What exactly you need help with? What is the base64 that you show in the Iframe, is it an image or PDF file?

If you show the binary in the IFRAME, where did it come from, and why you need to store it again?

Is it the problem that you don't know how to get the iframe content and make a binary?

Regards,

Daniel

Hi Daniel ...

actually I have a PDF inside my IFRAME: I would like a BUTTON (outside the IFRAME) to download that PDF.


LUCA

mvp_badge
MVP

Make sure the IFrame has a name set in OutSystems.

Make sure that the HTML with the PDF base64 inside the Iframe also has a name set.

Then you can use 

var iframe = document.querySelector('# + IframeWidgetName.Id');
var pdfBase64 = iframe.contentWindow.document.getElementById('#' + PDFHtmlEelementName.Id);

Daniel, thanks for your answer ...

- I have an expression (with the name framePDF) with value "<iframe height='100%' width='100%' frameBorder='0' src='" + varLink + "'></iframe>"

- my varLink has this value: "data:application/pdf;base64," + BinaryToBase64.Base64


On  my button action I put a "RunJavaScript" with your code inside:

"
var iframe = document.querySelector('# + framePDF.Id');
var pdfBase64 = iframe.contentWindow.document.getElementById('#' + PDFHtmlEelementName.Id);
"

I miss the last part (I never do that): how can I pass the value to my Base64ToBinary ?

2javascript.png

mvp_badge
MVP

Hi Luca,

Create an input widget on your screen and set the hidden style for it, make sure you also give the widget a name.

Then use document.getElementById('"+Input.Id+"').value = YourBase36StringValue;

Now the local variable bound to the Input widget has the value set.

Regards,

Daniel

Hi Daniel,

I have an error during the RunJavascript ... "An exception occurred in the client script. Error: iframe.contentWindow is undefined".

On my Service Center is logged as "Server cannot set content type after HTTP headers have been sent."

pic1.png

I attach even the second screenshot

pic2.png

I try to riassume everything:


In my page I have:

- Expression > name = framePDF
- IFRAME > id = iframePDF
- Input > name = inputPDF / variable = varPDF (visible = false)


In my Action I have:

- RunJavaScript with the following script:

"
var iframe = document.querySelector('#iframePDF.Id');
var pdfBase64 = iframe.contentWindow.document.getElementById('#" + framePDF.Id + "');
document.getElementById('" + inputPDF.Id + "').value = varPDF;
"

Hi Luca,

You probably get that error because the variable iframe isn't actually holding the iframe.

You have to set the id of the IFRAME itself, which you say you did (IFRAME Id = 'iframePDF').
This way you can access this as Daniel said. If you have a hardcoded id, you don't need to put '.id' behind it. So:
var iframe = document.querySelector('#iframePDF');

Can you check if you can now access the contentWindow?

Btw, I didn't see an id='iframePDF' tag in your example (<iframe .... /iframe>). Did you set the id?

... anyone else ?

Hi Lennart: today I fixed my script ... thanks.

That's the code I'm using:

"var iframe = document.querySelector('#iframePDF');
var pdfBase64 = iframe.contentWindow.document.getElementById('#viewerContainer');
document.getElementById('" + txtBase64.Id + "').value = pdfBase64;
"


- "iframePDF" is the ID of my iFrame

- "viewerContainer" is the DIV containing my PDF (I'm using Firefox ... so the PDF is rendered with PDF.js)

- "txtBase64" is the name of my hidden InputBox inside my page

htmlerror2.png

even with the option in my IFRAME ...


"<iframe id='iframePDF' height='100%' width='100%' frameBorder='0' crossorigin='anonymous' src='" + varLink + "'></iframe>"


I've got the error ... "An exception occurred in the client script. Error: Permission denied to access property "document" on cross-origin object"

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.