345
Views
17
Comments
Solved
Client side text to binary data conversation.

Hi guys,

In reactive web application i need to convert text to BinaryData on client side for that i have used forget component called TexttoBinaryData Client side but i am getting a window.btoa error.

please let know are there any solution to convert TexttoBinaryData on clientSide.

2021-01-04 08-13-48
Toto
 
MVP
Solution

Hi Nani,


The plugin is cannot handle UTF-8, so what I do is adding unescape(encodeURIComponent to the original plugin function of "TextToBinaryData" from the plugin (check the screenshot)


From what I test, this should works. I also attached the modified oml.

TestApiMod.oml
2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP

Hi Nani,

Next time you post a question about using a Forge component, select on the form where you post a question the Forge component. This makes it easier for people to understand which component, and also the team of the component gets a notification.

I could not find the component you refer to.

Regards,

Daniel

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP

That component does work for me perfectly.

Did you look at the Demo app on how to use it?

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP

Hi,

Please try the following Forge component, also has great demo app, and a Try Me button, so you can even see how it works before installing the component.

It does work, I can confirm that. You probably have to update the references on the demo app and publish it before trying.

https://www.outsystems.com/forge/component-overview/3242/binarydata-client-side

Regards,

Daniel

UserImage.jpg
Nani

Its giving me error.

I wrote my own Javascript 


var encoder = new TextEncoder('utf-8');

var binaryData = encoder.encode($parameters.Text);

var encodedText = window.btoa(String.fromCharCode.apply(null, binaryData));

$parameters.BinaryData = encodedText;


its working but giving stack error.

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP

Why you write your own JavaScript, to use the Forge component with success you need to follow the instructions as demonstrated in the Demo App. All you need to do is use BinaryAndText\TextToBinaryData

UserImage.jpg
Nani

I am having api output which is text data and i have to make changes in the text and convert into binary and store it in table.

That text will have special characters and emoji and symbols that forge component will fail and length can be dynamic present i am work on text having 4072624 Characters. 

example:

Text = "name":"🌈 Design ", "characters":"FAQ’s", "lastModified":"2023-12-26T10:39:44Z"

If you have time try to convert the text which i have given above, you will find out that component will raise a error.

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP

why do you need to convert this text (which isn't just text) to binary?

2025-02-09 19-39-40
Lucas Soares
 
MVP

Hi Nani, I hope you are well.

To transform text into binary using javascript, you can use :


const texto = "Hello, world!";

const bytes = texto.split("").map((c) => c.charCodeAt(0));

const arrayBuffer = ArrayBuffer.from(bytes);

const fileSaver = new FileSaver();

fileSaver.save(new Blob([arrayBuffer]));

$resolve();


You will need to replace the input and output parameters in the code.


2024-05-06 07-41-12
Narendra Tiwari

Hi nani,

you can also try this one hope this should help you.


Thanks,

Narendra Tiwari

2021-01-04 08-13-48
Toto
 
MVP

Hi @Narendra Tiwari 

The author is asking about converting binary data on client side, what your suggest is on server side.

Hope this helps as an additional information.

UserImage.jpg
Nani

Hi guys,

I have tried my best for client side conversation,  JavaScript raising error like scripts missing, librarys needed, stack Overflow, Window.btoa, etc...,

I know the extension works pretty good but it is a server side.

Well now i am using the extension but i have increased the server time out.

thank you for your all support.

In future if you find a solution please let know.

2021-01-04 08-13-48
Toto
 
MVP

Hi nani,


Can you give the oml ? I also use binarydata-client-side and it always works fine.

Let me check from your oml.

UserImage.jpg
Nani

Its not just a text there are also some symbols, special character, emoji's, etc..,

I have a created test screen for that. 

TestApi.oml
2021-01-04 08-13-48
Toto
 
MVP
Solution

Hi Nani,


The plugin is cannot handle UTF-8, so what I do is adding unescape(encodeURIComponent to the original plugin function of "TextToBinaryData" from the plugin (check the screenshot)


From what I test, this should works. I also attached the modified oml.

TestApiMod.oml
2014-10-21 20-15-17
Alberto Ferreira
2023-02-20 05-20-57
Nam Nguyen

Hi Nani, 

I just take a look at your code. 

btoa doesn't really handle UTF8-strings - it's ASCII only. You had fixed that by writing your own code.

About Maximum call stack size exceeded error : As you researched, it usually occurs when there is excessive recursion, leading to the call stack growing beyond its limit.  
In your code, it happens at String.fromCharCode.apply(null, binaryData). On large data like 4072624 Characters, using apply lead to that error.


I suggest to break the data into smaller chunk 

function test(Text) {

  var encoder = new TextEncoder('utf-8');

  var binaryData = encoder.encode(Text);


  var CHUNK_SIZE = 0x8000; // 32k  define your chunk size

  var chunks = [];

  for (var i = 0; i < binaryData.length; i += CHUNK_SIZE) {

    chunks.push(binaryData.slice(i, i + CHUNK_SIZE));

  }


  var encodedText = '';

  chunks.forEach(function(chunk) {

    encodedText += window.btoa(String.fromCharCode.apply(null, chunk));

  });


  return encodedText;

}




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