Skip to content
Upload and download files via BLOB in Business Central

How to Upload and Download Files in a Blob Field in Business Central

Daniel Gorski
Daniel Gorski January 29, 2025
4 min read

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:

https://github.com/byndit/BeyondCloudConnector-Example/blob/main/BeyondCC/src/MyBlobTable/MyArchive.Page.al

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:

  1. Import 🔼 for uploading a file into the BLOB.
  2. 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 🏁

  1. Create the Table 🗄️

    • Ensure you have a table named ABC My Archive with fields:
      • Entry No. (primary key)
      • File Name (Text)
      • File Content (BLOB)
  2. 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.
  3. 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.
  4. Export Action 🔽

    • Reads the BLOB using CreateInStream().
    • Downloads the file with its original File Name via DownloadFromStream().
  5. 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! ☀️

The app for this topic

Beyond Cloud Connector Logo

Beyond Cloud Connector

Business Central SharePoint integration, plus Azure Files and Blob Storage: offload documents, cut database bloat, access files from anywhere.

Beyond Cloud Connector

Related posts

▸ All posts
SETFILTER issue with special characters in Business Central

Why SETFILTER doesn’t work sometimes

May 04, 2023 · Daniel Gorski

SETFILTER is an AL staple, but it fails silently when the filter string contains special characters like * or @. Here is why, and how STRSUBSTNO fixes it.

Read More
Read More: Why SETFILTER doesn’t work sometimes
Business Central performance tips from BC TechDays

Performance Giveaways

September 19, 2022 · Daniel Gorski

Inspired by Waldo’s BC TechDays 2022 session – discover 5 practical AL coding tips to boost performance in Microsoft Dynamics 365 Business Central.

Read More
Read More: Performance Giveaways
Split multiline comments in Business Central

Split Multiline Text into Records

June 28, 2022 · Daniel Gorski

Learn how to split long multiline text into comment records in Business Central using AL and Regex — including a working editor example and useful patterns.

Read More
Read More: Split Multiline Text into Records