top of page
Writer's pictureTabrez Ajaz

How to Read file content in Dynamics 365 Business Central – AL Programming

Welcome Dynamics 365 BC Lovers,

In this article I will explain how to read content of a selected file from local. To achieve this i will create a simple standard dialog page with 1 fields, on it’s drilldown trigger wrote logic for select file from local to read.

Demonstration Video:


InStream: It is a generic stream object that you can use to read data from or write to files and BLOBs. You can define the internal structure of a stream as a flat stream of bytes. You can also assign one stream to another, and also reading from and writing to a stream both can occurs sequentially.

The InStream data type can be used to read bytes from a stream object. The data is read in binary format, and you can use the Read and ReadText functions to read that format.

UploadIntoStream: Sends a file from the client computer to the corresponding server. The client computer is the computer that is running the Windows client or the computer that is running a browser that accesses the web client.

Standard Dialog Page – Code Snippet: .

page 80027 ReadFileContent
{
    Caption = 'Read file content - Demo';
    PageType = StandardDialog;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field("Upload File and Read it's content"; selectFile)
                {
                    ApplicationArea = All;
                    Visible = false;
                    trigger OnDrillDown()
                    var
                        InStr: InStream;
                        FileName: Text;
                        NumberOfBytesRead: Integer;
                        TextRead: Text;
                    begin
                        // You can read from or write to streams by using the InStream and OutStream methods.The Temp Blob codeunit can be used to convert between the two stream types.
                        // The InStream data type can be used to read bytes from a stream object.The data is read in binary format, and you can use the Read and ReadText functions to read that format.
                        if (File.UploadIntoStream('Open File', '', 'All Files (*.*)|*.*',
                                                 FileName, InStr)) then begin
                            // If you use read then while written after read will not read anything because already everything in InStream variable is read -- vice versa
                            InStr.Read(TextRead);
                            Message(TextRead);

                            // Start: Read Each Line one by one
                            // while not InStr.EOS() do begin
                            //     NumberOfBytesRead := InStr.ReadText(TextRead, 100);
                            //     Message('%1\Size: %2', TextRead, NumberOfBytesRead);
                            // end;
                            // Stop: Read Each Line one by one
                        end;
                    end;
                }
            }
        }
    }

    var
        selectFile: Text; // File to Read
}
 

I hope you understand how to read content from a file in Dynamics 365 Business Central.

Stay Tuned!

417 views0 comments

Comments


bottom of page