Manage files through supported AL streams

Completed

Business Central online doesn't expose a service-tier file path to extension code. Streams let your logic read and write content without depending on where the client or service stores a file.

Transfer content with streams

An InStream reads bytes or text from a source. An OutStream writes content to a destination. A stream can represent an uploaded file, a binary large object (BLOB), a report result, or content held in a temporary blob.

Use UploadIntoStream to let a user select a local file in the web client. The runtime transfers the content into an InStream that AL can process.

var
    SourceStream: InStream;
    FileName: Text;
    ImportService: Codeunit "Item Review Import";
begin
    if not UploadIntoStream('CSV files (*.csv)|*.csv', SourceStream) then
        exit;

    ImportService.ImportItemReviews(FileName, SourceStream);
end;

When the overload doesn't return a file name, treat the name as optional metadata or use an overload that provides it. Don't use a local path as though the service can open a file on the user's computer.

Use CopyStream when content moves unchanged between streams. Use ReadText and WriteText when your code must interpret text. Specify an encoding when you create a stream over a blob and the external contract requires one.

Parse structured content with a supported API

Choose a parser that matches the file format. XMLports support structured imports and exports. JSON types support JSON payloads. The temporary CSV Buffer table handles comma-separated values (CSV), including format details that a simple Text.Split implementation can miss.

var
    TempCSVBuffer: Record "CSV Buffer" temporary;
    LineNo: Integer;
    ItemNoText: Text;
begin
    TempCSVBuffer.LoadDataFromStream(SourceStream, ',');

    for LineNo := 2 to TempCSVBuffer.GetNumberOfLines() do begin
        ItemNoText := TempCSVBuffer.GetValue(LineNo, 1);
        ProcessRow(LineNo, ItemNoText);
    end;
end;

The example starts at line two because line one holds headers. A production contract should define the delimiter, encoding, required columns, header names, date representation, maximum file size, and blank-line behavior.

Separate transport, parsing, and updates

Keep three concerns distinct:

  • Transport obtains an InStream from an upload, BLOB, or integration.
  • Parsing converts external text into typed AL values.
  • Application logic validates and changes Business Central records.

This separation allows the same import service to accept content from another source without duplicating record logic. It also lets parsing reject a malformed row before any database write begins.

For more information, see Streams overview and InStream data type.