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

mysamza
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;
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.
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.
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions