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

mysamzamysamza Member Posts: 66
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 table had the Entry after this code executed and if Tenant Media successfully saved the Base64 String as an image in Content. All seemed ok and Item Variant page showed the attachment on the drilldown page. However, it did not download the image upon clicking on the image.
Sign In or Register to comment.