Introduction 🌟
Working with file uploads and downloads in Business Central can feel like magic! ✨ Have you ever needed to store documents, images, or any other files directly in your Business Central database? If so, you’re in the right place! In this blog post, we'll show you how to create a custom page (in AL) that lets you upload and download files to a BLOB field. Let’s get started! 🚀
Get the full source code from here:
Understanding the Scenario 📝
We have a custom table called ABC My Archive that stores files in a File Content (BLOB) field, along with some metadata like Entry No. and File Name. We then expose this data on a List page (also called My Archive) and add two actions:
Import 🔼 for uploading a file into the BLOB.
Export 🔽 for downloading a file from the BLOB.
The AL Code Snippet 💻
Below is the complete page object code you can copy into your AL project. 🏗️
page 50001 "ABC My Archive"
{
ApplicationArea = All;
Caption = 'My Archive';
PageType = List;
SourceTable = "ABC My Archive";
UsageCategory = History;
Editable = false;
layout
{
area(Content)
{
repeater(General)
{
field("Entry No."; Rec."Entry No.")
{
ApplicationArea = All;
ToolTip = 'Specifies the value of the Entry No. field.', Comment = '%';
}
field("File Name"; Rec."File Name")
{
ApplicationArea = All;
ToolTip = 'Specifies the value of the File Name field.', Comment = '%';
}
field("File Content"; Rec."File Content".HasValue)
{
Caption = 'Blob Content';
ApplicationArea = All;
ToolTip = 'Specifies the value of the File Content field.', Comment = '%';
}
}
}
}
actions
{
area(Processing)
{
action(Import)
{
ApplicationArea = All;
Promoted = true;
Image = Import;
Caption = 'Import';
trigger OnAction()
var
InStr: InStream;
OutStr: OutStream;
Filename: Text;
begin
if not UploadIntoStream('Upload File', '', '', Filename, InStr) then
exit;
Rec.Init();
Rec."File Content".CreateOutStream(OutStr, TextEncoding::Windows);
Rec."File Name" := Filename;
CopyStream(OutStr, InStr);
Rec.Insert(true);
end;
}
action(Export)
{
ApplicationArea = All;
Promoted = true;
Image = Export;
Caption = 'Export';
trigger OnAction()
var
InStr: InStream;
Filename: Text;
begin
Filename := Rec."File Name";
Rec.CalcFields("File Content");
if not Rec."File Content".HasValue then
exit;
Rec."File Content".CreateInStream(InStr, TextEncoding::Windows);
DownloadFromStream(InStr, 'Download', '', '', Filename);
end;
}
}
}
}
Step-by-Step Guide 🏁
Create the Table 🗄️
Ensure you have a table named ABC My Archive with fields:
Entry No. (primary key)
File Name (Text)
File Content (BLOB)
Add the Page 📄
Create a new page (as shown above) with the source table ABC My Archive.
Include the Import and Export actions in the actions section.
Import Action 🔼
Uses UploadIntoStream() to let users pick a file.
Writes the uploaded file into the File Content (BLOB) field using CreateOutStream() and CopyStream().
Inserts a new record with the chosen File Name.
Export Action 🔽
Reads the BLOB using CreateInStream().
Downloads the file with its original File Name via DownloadFromStream().
Publish Your Extension 🚢
Save and publish your AL project to make the new page and actions available in Business Central.
Test uploading and downloading files to confirm everything’s working.
Tips & Tricks 🤓
Text Encoding: Notice we used TextEncoding::Windows. This might be adjusted based on your file requirements or system defaults.
File Type: The file extension in the Filename helps keep track of the file format during download.
Security: Storing files in BLOB fields keeps them within your Business Central database, so ensure you have proper backup and security strategies in place.
Final Thoughts 💡
Congratulations! 🎉 You've just learned how to upload and download files in Business Central using a BLOB field. This approach can help you store and retrieve important documents, images, or other assets right from within your app.
Keep exploring, keep innovating, and have fun building solutions that make your workflows easier and more efficient! 💪🌈
Happy AL coding! ☀️
Comments