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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Inserting Base64 image string into Document Attachment Table and Tenant Media.

(0) ShareShare
ReportReport
Posted on by 358

I get a Base64 String which is an image from an API. Which will be saved into Document Attachment table.

Here is the high level;

Using this https://www.olisterr.tech/2019/12/generic-way-of-attaching-documents-on.html instead of having Document Attachment functionality on Opportunity, I followed the example to have it on Item Variant.

All was good. There is factbox on Item Variant that allows users to upload files as attachments to each Variant Code.

Now, the idea is to have it uploaded into Item Variant through the API that gives me the image as Base64, so here is the code I attempted knowing I have to save it in table Document Attachment and Tenant Media;

      
         rec_DocumentAttachment: Record "Document Attachment";
        rec_DocumentAttachment2: Record "Document Attachment";
        Base64CU: Codeunit "Base64 Convert";
           Url: Text;
              Object1: JsonArray;
        JsonText: Text;
          Client: HttpClient;
        ResponseMessage: HttpResponseMessage;
          VarOutStream: OutStream;
        imagenameT: Text;
        base64T: Text;
              rec_TenantMedia: Record "Tenant Media";
      begin
      
       Url := 'https://pure-earth-53603.herokuapp.com/variants';
        if not client.Get(Url, responseMessage) then
            Error('The call to the web service failed.');
        if not ResponseMessage.IsSuccessStatusCode then
            Error('The web service returned an error message:\\'  
           'Status code: %1\'  
            'Description: %2',
          ResponseMessage.HttpStatusCode,
            ResponseMessage.ReasonPhrase);
        ResponseMessage.Content.ReadAs(JsonText);
        
            jFileName := GetArrayElementAsObject(Object1, 0, 'err');
                imagenameT := GetTokenAsText(jFileName, 'imagename', 'err');
                base64T := GetTokenAsText(jFileName, 'imagebase64', 'err');
                rec_DocumentAttachment.Init();
                rec_DocumentAttachment."Table ID" := 5401;
                rec_DocumentAttachment."No." := imagenameT;
                rec_DocumentAttachment2.SetRange("Table ID", 5401);
                rec_DocumentAttachment2.SetRange("No.", imagenameT);
                if rec_DocumentAttachment2.FindLast() then begin
                    rec_DocumentAttachment."Line No." := rec_DocumentAttachment2."Line No."   1000;
                    rec_DocumentAttachment.ID := rec_DocumentAttachment2.ID   1;
                end;
                rec_DocumentAttachment."File Name" := imagenameT;
                rec_DocumentAttachment."Attached By" := '{9e089eeb-4e51-49ec-be35-0d203e241c49}';
                rec_DocumentAttachment."Attached Date" := CurrentDateTime;
                rec_DocumentAttachment."File Type" := rec_DocumentAttachment."File Type"::Image;
                rec_DocumentAttachment."File Extension" := 'jpg';
                rec_DocumentAttachment.VariantAttachment := true;
                   rec_DocumentAttachment.Insert();
                   if rec_DocumentAttachment.FindLast() then;
                   rec_TenantMedia.Init();
                   rec_TenantMedia.ID := CreateGuid();
                   rec_TenantMedia.Description := imagenameT;
                   rec_TenantMedia."Mime Type" := 'image/jpeg';
                   rec_TenantMedia."Company Name" := COMPANYNAME;
                   rec_TenantMedia."File Name" := imagenameT   '.jpg';
                   rec_TenantMedia.Height := 500;
                   rec_TenantMedia.Width := 500;
                   rec_TenantMedia.CalcFields(Content);
                   rec_TenantMedia.Content.CreateOutStream(VarOutStream);
                   Base64CU.FromBase64(base64T, VarOutStream);
                   rec_TenantMedia.Insert(); 
        
        end;

Now I went ahead to see if my Document Attachment had the Entry after this code executed, and in Tenant Media successfully saved the Base64 String as an image in Content blob filed. All seemed ok and Item Variant showed the attachment on the drilldown page of the attachment. However, it did not download the image upon clicking on the image.  

  • Suggested answer
    Olister Rumao Profile Picture
    4,009 on at

    Hi Mesam,

    Are you sure if Export works for other process on not explicitly for this process?

    I think I will have to evaluate and check what is going wrong specifically this case.

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 664 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 446 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 282 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans