I am using On-Prem Business central.
I have created on custom API which is returning the item detail with item picture in base64 string. but it is taking long time to fetch data using API(45 second). Due to the large base64 string it is taking long time to process.
Does anyone have any idea how i can optimize it.
Or anyone have how idea how i can get exact URL of image which i can set in the image tag Source. eg. <img src="itemURL">.
Code :
{
PageType = API;
APIVersion = 'v1.0';
APIPublisher = 'QueryV1';
APIGroup = 'QueryGroup';
EntityName = 'Item';
EntitySetName = 'Items';
DelayedInsert = true;
SourceTable = Item;
layout
{
area(content)
{
repeater(Group)
{
field(ItemNo; Rec."No.")
{
Caption = 'Item No';
}
field(BrandName; Rec."Brand Name")
{
Caption = 'Brand Name';
}
field(Base_Unit_Price; Rec."Unit Price")
{
Caption = 'Base Unit Price';
}
// Picture content in base64
field(PictureContent; GetItemPicturesAsJSON(Rec."No."))
{
Caption = 'Picture Content';
ApplicationArea = All;
}
}
}
}
// Procedure to get base64 string of image.
procedure GetItemPicturesasJSON(ItemNo: Code[20]): Text;
var
Item: Record Item;
TenantMedia: Record "Tenant Media";
PictureText: Text;
PictureInStream: InStream;
JObject: JsonObject;
JToken: JsonToken;
TempBlob: Codeunit "Temp Blob";
PictureOutstream: OutStream;
Base64Convert: Codeunit "Base64 Convert";
begin
Item.get(ItemNo);
if Item.Picture.Count = 0 then
exit('');
TenantMedia.CalcFields(Content);
Clear(PictureText);
Clear(PictureInStream);
TenantMedia.Content.CreateInStream(PictureInStream);
TempBlob.CreateOutStream(PictureOutstream);
CopyStream(PictureOutstream, PictureInStream);
TempBlob.CreateInStream(PictureInStream);
PictureText := Base64Convert.ToBase64(PictureInStream, false);
JObject.Add('picture', PictureText);
JObject.SelectToken('picture', JToken);
end;
exit(JToken.AsValue.AsText);
end;
Thanks for help.