top of page
Writer's pictureTabrez Ajaz

How to download any text content in File using Dynamics 365 Business Central? InStream, OutStream an

Updated: Feb 21, 2022

Welcome Dynamics 365 BC Lovers,


In this article, I will explain how to download any text content in the file, but if you want to store the content in any Business Central table Blob field you can refer to my other post in this post I already explained how to read/write in Blob fields. To achieve this I will create a simple standard dialog page with 2 fields, in one field enter your file name with extension, and in another enter any text that you want in the file.


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


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


Demonstration Video:


Standard Dialog Page – Code Snippet: Used TempBlob record is not available in the later version of 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 record.

page 80027 WriteAndDownloadAnyText
{
    Caption = 'Write anything and download txt file - Demo';
    PageType = StandardDialog;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field("File Name including extension"; gFileName)
                {
                    ApplicationArea = All;
                }
                field("Write your text in this box - And - Leave the Box"; writtenText)
                {
                    ApplicationArea = All;
                    MultiLine = true;

                    trigger OnValidate()
                    var
                        Data: BigText;
                        ins: InStream;
                        outs: OutStream;
                        TempBLOB: Record TempBlob;
                        filename: Text;
                    begin                        
                        Data.AddText(writtenText);
                        TempBLOB.Blob.CreateOutStream(outs);
                        Data.Write(outs);
                        TempBLOB.Blob.CreateInStream(ins);
                        filename := gFileName;
                        // Sends a file from server computer to the client computer. The client computer is the computer that is running the Windows client or the computer that is running the browser that accesses the web client.
                        DownloadFromStream(
                            ins,  // InStream to save
                            '',   // Not used in cloud
                            '',   // Not used in cloud
                            '',   // Not used in cloud
                            filename); // Filename is browser download folder
                    end;
                }
            }
        }
    }

    var
        writtenText: Text; // Your text
        gFileName: Text; // File name to download
}
 

I hope you understand how to download any text content in Dynamics 365 Business Central.


Stay Tuned!

790 views0 comments

Comments


bottom of page