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
Paul Mulroney replied 3 days, 14 hours ago 4 Members · 8 Replies -
8 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 5 days ago by Martin FF.
-
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
-
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
Log in to reply.