web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Appending multiple streams in a loop

(1) ShareShare
ReportReport
Posted on by 891
I've written a procedure that is intended to loop through a filtered recordset of "Document Attachment" records and append the contents of
field(8; "Document Reference ID"; Media)
 on each record into a single stream. However, in the code below when it gets to 
OutS.Write(InS);
 it throws error: The type InStream is not supported.
Where am I going wrong?
 
    local procedure ReadAttachments(var InS: InStream; DocumentRef: RecordRef; iTableNo: Integer; txtNoFieldName: Text): Boolean
    var
        recDA: Record "Document Attachment";
        fldRef: FieldRef;
        iNoFieldNo: Integer;
        txtNoValue: Text;
        OutS: OutStream;
        TempBlobAtt: Codeunit "Temp Blob";
        Buffer: array[1024] of Byte;
        ReadBytes: Integer;
        bReturnValue: Boolean;
    begin
 
        bReturnValue := false;
        Clear(InS);
 
        // initialise OutS
        TempBlobAtt.CreateOutStream(OutS, TextEncoding::Windows);
 
        iNoFieldNo := GetFieldNumber(txtNoFieldName, iTableNo);
        recDA.SetFilter("Table ID", Format(iTableNo));
        fldRef := DocumentRef.Field(iNoFieldNo);
        txtNoValue := fldRef.Value;
        recDA.SetFilter("No.", txtNoValue);
        if recDA.FindSet() then begin
            bReturnValue := true;
            repeat
                // Create OutStream from "Document Attachment" object.
                // In other words, export the bytes of the "Document Attachment" database field into the OutStream.
                recDA."Document Reference ID".ExportStream(OutS);
 
                // Tie InS (InStream) to OutS (OutStream) via TempBlob object
                TempBlobAtt.CreateInStream(InS, TextEncoding::Windows);
 
                // *** This line is erroring ***
                OutS.Write(InS);
                // *****************************
 
            until recDA.Next() = 0;
 
            // Now OutS contains data from all attachments
 
            // Create InStream from the same TempBlob
            // Clear(InS);
            TempBlobAtt.CreateInStream(Ins);
 
            // Now Ins contains the concatenated data
        end;
    end;
 
Many thanks in advance, for any help provided.
 
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,763 Super User 2025 Season 2 on at
    Hi, hope the following can give you some hints.
    Dynamics 365 Business Central: How to bulk export/download attachments (Downloading multiple attachments via zip file) – Customization
     
    Thanks.
    ZHU
  • Nick Webb Profile Picture
    891 on at
    Thanks for your response and this is a useful bit of code, but it still leaves each attachment as a separate entity inside the zip-file. I was hoping to achieve one file with the contents of multiple concatenated attachments streamed into one. Is this not possible? I'm aware that each attachment might be a different type (e.g. PDF, JPG etc..), but in my case they're all PDF's.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,116

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 764 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 635 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans