top of page
Writer's pictureTabrez Ajaz

Read file content and add some your content and download the file in Dynamics 365 Business Central

Updated: Feb 21, 2022

Welcome Dynamics 365 BC Lovers,

In this article, I will explain how to read the content of a selected file from local then add your own content and download a new file with existing and newly added content. To achieve this I will create a simple standard dialog page with 3 fields, in the first field drilldown trigger wrote logic for selecting file from local to read, in the second field type your text content, and in the third enter a new filename to download.


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 occur 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.


OutStream: This is a generic stream object that you can use to write to files and BLOBs. You can use the Write and WriteText function to wrote binary format data.


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: Used TempBlob record is not available in the later version of Business Central (BC14) or in the Online environment, so if you are not using BC14 then please make sure to use TempBlob as Codeunit datatype, not as a record.

page 80027 WriteContentToFile
{
    Caption = 'Write content to existing file - Demo';
    PageType = StandardDialog;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field("You Text Content"; TextContent)
                {
                    ApplicationArea = All;
                    MultiLine = true;
                }
                field("New File Name"; gFileName)
                {
                    ApplicationArea = All;
                }
                field("Select File"; selectFile)
                {
                    ApplicationArea = All;

                    trigger OnDrillDown()
                    var
                        InStr: InStream;
                        FileName: Text;
                        NumberOfBytesRead: Integer;
                        TextRead: Text;
                        OutStr: OutStream;
                        TempBlob: Record TempBlob;
                    begin
                        //                                         'Excel Files (*.xlsx)|*.xlsx'
                        if (File.UploadIntoStream('Open File', '', 'All Files (*.*)|*.*',
                                                 FileName, InStr)) then begin
                            InStr.Read(TextRead);
                            Message('Previos file content: \' + TextRead);
                            TempBlob.Blob.CreateOutStream(OutStr);
                            OutStr.WriteText(TextRead);
                            OutStr.WriteText(' new text');                            
                            TempBlob.Blob.CreateInStream(InStr);
                            TextRead += ' --------- Your Content ---------- ' + TextContent;
                            InStr.Read(TextRead);
                            Message('New File content: ' + TextRead);                            
                            DownloadFromStream(InStr, '', '', '', gFileName); // Filename is browser download folder
                        end;
                    end;
                }
            }
        }
    }

    var
        selectFile: Text; // File to Read
        TextContent: Text; // Content to write in file
        gFileName: Text; // New Filename to download file

}
 

I hope you understand how to read content from a file, write new content and download another file with newly added content in Dynamics 365 Business Central.


Stay Tuned!

857 views0 comments

Comments


bottom of page