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

Announcements

No record found.

News and Announcements icon
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 905
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
    97,970 Super User 2026 Season 1 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
    905 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,161 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,046 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 932 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans