Convert Base64 with AL code

10 Jul

Update June 13th, 2020: In this video, Erik Hougaard discusses this code and improves the performance dramatically by making use of the TextBuilder variable. He is building up a great library of very useful video’s, so make sure to check out his YouTube channel!

A couple of weeks ago, I was trying to write web service examples in AL code for VS Code. Unfortunately, at that time, it was not possible to call the Base64 methods on the TempBLOB table. And because those functions on the TempBLOB table are using .Net, I figured it was going to take a while before we get Base64 support in AL code. So I decided to write Base64 encoding / decoding routines myself. Just as an excercise and at that moment I didn’t know if I would even succeed.

Well, it turned out that it was possible and I ended up with a Codeunit that was able to convert a string to Base64 and also back into a string. At that moment, I already read in a GitHub issue that Microsoft promised to make the Base64 functions on TempBLOB table available in the next update. So I decided to not publish the code and wait for the next update.

However, a new question came up about Base64. And apparently, people were still interested in having the code. Because they were talking about a file I figured the code should not only support strings, but also binary data. That was another challenge that I just wanted to pick up. So I did. The Codeunit is now supporting both text and binary data. For binary data the code works with InStream and OutStream variables. In that way there is no dependency on any table.

Base64Code

I’m not going into the details of Base64 encoding itself. A great resource can be found here and here. I used these sites to understand how Base64 encoding really works and translated it to AL code.

You can download the code from GitHub.

7 thoughts on “Convert Base64 with AL code

  1. Pingback: Convert Base64 with AL code - Kauffmann @ Dynamics NAV - Dynamics NAV Users - DUG

  2. Hi Kauffmann
    Thank you for such an informative post. Can you please tell me if it is possible to change StreamToBase64String(instream) function’s parameter to Outstream? Will it make any difference to the logic?

    Thanks in advance 🙂

  3. Hi AJ, thanks for sharing this article & codeunit.
    I just tried using this to export all images in base64string in JSON (as per some requirement).
    It works but takes a lot of time to produce a json of all images and export the same.

    Item.Reset();
    If Item.FindSet() then begin
    repeat
    If Item.Picture.Count 0 then
    for i := 1 to Item.Picture.Count do begin
    if TenantMedia.Get(Item.Picture.Item(i)) then begin
    TenantMedia.CalcFields(Content);
    if TenantMedia.Content.HasValue() then begin
    TenantMedia.Content.CreateInStream(PictureInStream);
    PictureTextBase64 := Base64Convert.StreamToBase64String(PictureInStream);
    JObject.Add(Item.”No.”, PictureTextBase64);
    end;
    end;
    end;
    until Item.Next() = 0;

    It usually takes much time in going byte by byte in “StreamToBase64String”.
    Is there any way to make it quicker, thanks!

    • Hi AJ, just realized that standard Codeunit “Base64 Convert” method can also overload and take Instream.
      This fixed my issue, thanks!

Leave a Reply to chavansachin20 Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.