Home › Forum › Omnis General Forum › Picture upload via API
-
Picture upload via API
Posted by Martin FF on May 3, 2024 at 10:38 am<div>Hello all,</div>
We have to send picture data via a API-POST.
The API requires base64 encoding.
I started with:
Do OXML.$base64encode(“C:\somepicture.jpg”,lErrorText) Returns lBinary
thinking this is the right start.
Can anyone help me with how to get the result in the format I need witch must be something like this:
“data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAA…gwQQR1GB//Z”
or this
“/9j/4AAQSkZJRgABAQAA…gwQQR1GB//Z”
Regards,
Martin
Deleted User replied 10 months, 2 weeks ago 6 Members · 10 Replies -
10 Replies
-
OXML.$base64encode requires the binary data of the picture. You have to load the data using FileOps first. For example
Do FileOps.$readfile(“path_to_image”,pictData)
Do OXML.$base64encode(pictData,err) Returns base64
Both pictData and base64 variables are of type binary
-
Hi Michael,
Thx for the quick reply,
The problem is not to to read the file but to get it in to readeble charakter data for the api as in my example.
-
-
It’s been a while, but this should work:
Calculate lString as utf8tochar(lBinary)
-
Try something like this:
Calculate vsImageEncoded as utf8tochar(bintobase64(vrResult.delSignature)) ## bintobase64 returns UTF8 ready to send, but we need char because we're building the file ourselves.
Then you can append this to the start
Calculate vsImageEncoded as con(‘data:image/jpeg;base64,’,vsImageEncoded)
Good luck!
Regards,
Paul.
-
Hi Paul,
Thank you for this answer. I went away for a weekend so I just read this post.
Let me put some examples in PHP and a genarator with the picture of Omnis(https://www.omnis.net/wp-content/uploads/2018/11/omnis-Logo.png) as data.
—————————————————————————–
In PHP we have:
$imagedata = file_get_contents(“https://www.omnis.net/wp-content/uploads/2018/11/omnis-Logo.png”);
$base64 = base64_encode($imagedata);
echo $base64 ;This result in the data you see if you open https://elemart.nl/base64/
—————————————————————————–
If we use a generator like https://www.base64-image.de/ and we use the “Or click here”-button, to enter https://www.omnis.net/wp-content/uploads/2018/11/omnis-Logo.png again with the same result as in PHP.
—————————————————————————–
Thereafter if I use your code the contens of the data is diffrent.
<div style=”font-family:Verdana,Arial,Helvetica,Sans-serif;font-size:14pt”>
Do OXML.$base64encode(“https://www.omnis.net/wp-content/uploads/2018/11/omnis-Logo.png”,lErrorText) Returns lBinary
Calculate vsImageEncoded as utf8tochar(bintobase64(lBinary))
Breakpoint
Calculate vsImageEncoded as con(‘data:image/png;base64,’,vsImageEncoded)
Breakpoint
Regards,
Martin
</div>
-
This reply was modified 1 year, 2 months ago by
Martin FF.
-
This reply was modified 1 year, 2 months ago by
-
Hi Martin,
You don’t need the first part – you just need to get the contents of the file into the variable lbinary, and then do this:
Calculate vsImageEncoded as utf8tochar(bintobase64(lBinary))
Calculate vsImageEncoded as con('data:image/png;base64,',vsImageEncoded)How you populate lbinary depends on whether you can read the file from disk, or retrieve it from a URL.
To read it from disk, do something like:
Do voFileOps.$openfile('path to the file')
Do voFileOps.$readfile(lbinary)
Do voFileOps.$closefile()If you need to fetch it from a website, you need something like this:
HTTPPage ('https://www.omnis.net/wp-content/uploads/2018/11/omnis-Logo.png') Returns lbinary ## fetches header and content
Calculate lbinary as bytemid(lbinary,binsearch(chartoutf8(chr(13,10,13,10)),lbinary)+4,binlength(lbinary)) ## strip off the header and leave the contentRegards,
Paul
-
Dear Paul,
dear $all,I have a similar problem with the REST API – maybe you have an idea on how to solve it:
https://www.omnis.net/community/forums/forum/discussion/rest-api-client-post-file/
-
-
I recommend you for a medal tomorrow at the Omnis event in the hotel tomorrow.
<div>Thx Paul!!</div>Just one thing for a local file to get, I had to use:
Do FileOps.$readfile(‘Path to File’,lBinary) -
Hi Martin,
Glad it worked for you!
Sorry I can’t make it to the conference, would’ve loved to be there but timing didn’t work for us.
Regards,
Paul
-
Deleted User
Deleted UserSeptember 19, 2024 at 4:08 am-
Base64 Encode the Image: You’re already using <code data-code-tools=””>OXML.$base64encode(“C:\somepicture.jpg”, lErrorText)
, which is correct to get the binary data encoded.
-
Format the Base64 String: After obtaining the base64 string, you need to prepend the appropriate prefix if you want the full data URL format.
Here’s a sample code snippet that demonstrates how to do fnaf:
plaintext
Copy
<code data-code-tools="">// Step 1: Base64 encode the image Do OXML.$base64encode("C:\somepicture.jpg", lErrorText) Returns lBinary // Step 2: Check for errors If lErrorText <> "" Then // Handle the error Return lErrorText End If // Step 3: Convert binary to base64 string lBase64String = OXML.$binaryToBase64(lBinary) // Step 4: Create the data URL format lDataUrl = "data:image/jpeg;base64," + lBase64String // Now lDataUrl contains the image in the required format
fngames.io
FNAF Game | Five Nights At Freddy's
FNAF (Five Nights At Freddy's) is a survival horror game that takes place at Freddy Fazbear's Pizza. Jump into the horror world with the best games like Backrooms, Poppy Playtime, Granny, and more.
-
Log in to reply.