Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Appending multiple streams in a loop

Posted on by 668
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.
 
  • Nick Webb Profile Picture
    Nick Webb 668 on at
    Appending multiple streams in a loop
    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.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 71,839 Super User 2024 Season 2 on at
    Appending multiple streams in a loop
    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

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,336 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans