Forums

Find answers, ask questions, and connect with our
community all around the world.

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
  • Michael Monschau

    Member
    May 3, 2024 at 11:02 am

    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

    • Martin FF

      Member
      May 3, 2024 at 11:21 am

      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.

  • Dario L.

    Member
    May 3, 2024 at 12:08 pm

    It’s been a while, but this should work:

    Calculate lString as utf8tochar(lBinary)

  • Paul Mulroney

    Member
    May 3, 2024 at 12:37 pm

    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.

  • Martin FF

    Member
    May 13, 2024 at 1:34 pm

    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&#8221;);
    $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&#8221;,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.
  • Paul Mulroney

    Member
    May 14, 2024 at 1:47 am

    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 content

    Regards,

    Paul

  • Martin FF

    Member
    May 14, 2024 at 1:58 pm

    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)


  • Paul Mulroney

    Member
    May 14, 2024 at 11:21 pm

    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.