Dynamics 365 Business Central: How to bulk export/download attachments (Downloading multiple attachments via zip file) – Customization

Dynamics 365 Business Central

Hi, Readers.
Yesterday we briefly discussed how to copy attachments from Warehouse Receipt to Posted Warehouse Receipt – Customization. I then received a question, is there a way to export (download) zip of files from attachments? So today I would like to talk about this briefly.

On most list pages, cards, and documents, you can attach files, add links, and write notes on the Attachments tab of the FactBox pane. The number in the tab title indicates how many attached files, links, or notes exist for the card or document.

This is a very handy feature, but so far, there are two small problems.
1. Uploading multiple files is not allowed.
More details: Drag and drop files onto the file upload dialog

We have discussed this prblem before, more details: How to import files from a zip file (Bulk import attachments and Excel files)

2. When you select multiple attachments, the Download action will be grayed out and we will not be able to download multiple attachments at the same time.

In this post, we will mainly deal with the second small problem. The solution is also simple, we can archive the files into a zip file first, then download it.
When we discussed FileManagement.BLOBExport only save last file? -> Adding multiple files into a zip file, we mentioned it briefly.

For example, Download selected attachments.

Test Video: Very simple😁.

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50122 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
    actions
    {
        addafter(Preview)
        {
            action(BulkDownload)
            {
                Caption = 'Bulk Download';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    TempBlob: Codeunit "Temp Blob";
                    FileManagement: Codeunit "File Management";
                    DocumentOutStream: OutStream;
                    DocumentInStream: InStream;
                    ZipOutStream: OutStream;
                    ZipInStream: InStream;
                    FullFileName: Text;
                    DataCompression: Codeunit "Data Compression";
                    ZipFileName: Text[50];
                    DocumentAttachment: Record "Document Attachment";
                begin
                    ZipFileName := 'Attachments_' + Format(CurrentDateTime) + '.zip';
                    DataCompression.CreateZipArchive();
                    DocumentAttachment.Reset();
                    CurrPage.SetSelectionFilter(DocumentAttachment);
                    if DocumentAttachment.FindSet() then
                        repeat
                            if DocumentAttachment."Document Reference ID".HasValue then begin
                                TempBlob.CreateOutStream(DocumentOutStream);
                                DocumentAttachment."Document Reference ID".ExportStream(DocumentOutStream);
                                TempBlob.CreateInStream(DocumentInStream);
                                FullFileName := DocumentAttachment."File Name" + '.' + DocumentAttachment."File Extension";
                                DataCompression.AddEntry(DocumentInStream, FullFileName);
                            end;
                        until DocumentAttachment.Next() = 0;
                    TempBlob.CreateOutStream(ZipOutStream);
                    DataCompression.SaveZipArchive(ZipOutStream);
                    TempBlob.CreateInStream(ZipInStream);
                    DownloadFromStream(ZipInStream, '', '', '', ZipFileName);
                end;
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL