Hi, Readers.
Today I would like to share another mini tip about Business Central, how to format File Size as KB, MB in AL.
As you might know, for file size, we can use the InStream.Length() Method (Gets the stream length) or procedure Length() in codeunit 4100 “Temp Blob”. More details: Better InStream support (InStream.Length & InStream.Position)
The InStream.Length() Method
allows you to get the length in bytes of the stream.
More details: Business Central 2023 wave 1 (BC22): Better InStream support (InStream.Length & InStream.Position)
procedure Length() in codeunit 4100 “Temp Blob” is the same.
For example,
More details: Dynamics 365 Business Central: How to limit max file size for uploading attachment (Customization)
So is there any simple way to do the following?
1024 ==> “1 KB”;
2537253 ==> “2.4 MB”
Unfortunately, as of now there is no standard method like the following in AL. Formatter.FormatFileSize(Context, Int64) Method
Of course, it is not difficult to write a method manually from scratch, but we have a reference point in standard. Have you noticed that when sending emails from BC, there is a File Size field in Attachments, which shows KB and MB on the .
Email Attachments (8889, ListPart):
How is this done? Here is a standard method:
codeunit 8905 “Email Message Impl.” -> internal procedure FormatFileSize(SizeInBytes: Integer): Text
FileSizeTxt: Label '%1 %2', Comment = '%1 = File Size, %2 = Unit of measurement', Locked = true;
// Used for formatting a filesize in KB or MB (only)
internal procedure FormatFileSize(SizeInBytes: Integer): Text
var
FileSizeConverted: Decimal;
FileSizeUnit: Text;
begin
FileSizeConverted := SizeInBytes / 1024; // The smallest size we show is KB
if FileSizeConverted < 1024 then
FileSizeUnit := 'KB'
else begin
FileSizeConverted := FileSizeConverted / 1024; // The largest size we show is MB
FileSizeUnit := 'MB'
end;
exit(StrSubstNo(FileSizeTxt, Round(FileSizeConverted, 1, '>'), FileSizeUnit));
end;
Although it cannot be called directly, we can refer to it to make some improvements. Here is a simple example, get the size of the file being uploaded
Test:
Or
Test video:
Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)
pageextension 50113 CustomerListExt extends "Customer List"
{
actions
{
addafter("C&ontact")
{
action(FormatFileSize)
{
Caption = 'Format File Size';
ApplicationArea = All;
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
trigger OnAction()
var
FromFile: Text[100];
IStream: InStream;
UploadMsg: Label 'Please Choose the file.';
begin
UploadIntoStream(UploadMsg, '', '', FromFile, IStream);
Message(FormatFileSize(IStream.Length));
end;
}
}
}
var
FileSizeTxt: Label '%1 %2', Comment = '%1 = File Size, %2 = Unit of measurement', Locked = true;
local procedure FormatFileSize(SizeInBytes: Integer): Text
var
FileSizeConverted: Decimal;
FileSizeUnit: Text;
begin
FileSizeConverted := SizeInBytes / 1024; // The smallest size we show is KB
if FileSizeConverted < 1024 then
FileSizeUnit := 'KB'
else begin
FileSizeConverted := FileSizeConverted / 1024; // The largest size we show is MB
FileSizeUnit := 'MB'
end;
exit(StrSubstNo(FileSizeTxt, Round(FileSizeConverted, 0.1, '>'), FileSizeUnit));
end;
}
Very simple, give it a try!!!😁
PS:
1. The maximum size of files that can be uploaded to or downloaded from Business Central is 350 MB. So I don’t think it’s necessary to convert to GB. More details: Data handling limits
END
Hope this will help.
Thanks for reading.
ZHU
コメント