web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Convert FROM Base64 to Binary in Business Central Version 17/18/19

(0) ShareShare
ReportReport
Posted on by 20

Hi all.

I am currently working with an import of a file in BC, where the file is a Base64 string, which i am placing in a media field in a Table.

It seems to me that the older version of convert, works ok here, but not the new codeunit..

the old conversion I used, is part of the TempBLOB table, which is discontinued in V19. the procedure I refer to here is FromBase64, and it seem to work OK.

The newer codeunit 'Convert Base64' has the similar procedure 'FromBase64', and is intended to replace the older Table code.

Now this again, produces a binary streing, and I get a value in my media field, but the data seem to be somehow corrupt here. (I use PDF files to show, and the come as blank pages)

Here is my code for the old, working version example:

var Base64Value, is a text string which contain the Base64 string of a pdf file.

            var

                attachBlob: Record "TempBlob";
                attachOut: OutStream;
                attachIn: InStream;
            FileName := 'File.pdf';
            AttachBlob.Blob.CreateInStream(attachIn);
            AttachBlob.Blob.CreateOutStream(attachOut);
            attachBlob.FromBase64String(Base64Value);
            media.keyId := media.MediaStream.ImportStream(attachIn, mergedFileName, 'application/pdf');
            media.Name := mergedFileName;
            media.Size := attachBlob.Length();
            media.Insert();

Here is my code for the new version example, not working, in my opinion:

            var

                base64: Codeunit "Base64 Convert";
                attachBlob: Codeunit "Temp Blob";
                attachOut: OutStream;
                attachIn: InStream;
            FileName := 'File.pdf';
            AttachBlob.CreateInStream(attachIn);
            AttachBlob.CreateOutStream(attachOut);
            attachOut.Write(base64.FromBase64(Base64Value));
            media.keyId := media.MediaStream.ImportStream(attachIn, mergedFileName, 'application/pdf');
            media.Name := mergedFileName;
            media.Size := attachBlob.Length();
            media.Insert();

Have anyone else touched this issue? 

Best Regards

Glenn

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: Convert FROM Base64 to Binary in Business Central Version 17/18/19

    Hello,

    Thank you so much for coming back with final answer. Yes the blogs from Kauffman or GitHub postings are really good.

    Thanks.

  • Glenn72 Profile Picture
    20 on at
    RE: Convert FROM Base64 to Binary in Business Central Version 17/18/19

    Hello Marco, and thanks for the reply and feedback..

    (Took a while, but) was able to find a solution, made by mr. Kauffman:

    github.com/.../Base64Convert

    this solution seemed to work a bit better then the base code, for my part..

    Best Regards

    Glenn

  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: Convert FROM Base64 to Binary in Business Central Version 17/18/19

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 3,406

#2
Sumit Singh Profile Picture

Sumit Singh 2,852

#3
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 2,217

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans