Bulk Export Item Pictures in Business Central SaaS version as Zip File

 you can use the below code as it is for your test environment.


    Procedure DownloadItemImage()
    var
        MyItem: Record Item;
        TenantMedia: Record "Tenant Media";
        datacompresion: Codeunit 425;
        blobStorage: Codeunit "Temp Blob";
        PicInStream: InStream;
        ZipInStream: InStream;
        ZipOutStream: OutStream;
        ZipFileName: Text;
        ItemCnt: Integer;
        Index: Integer;
        PicCount: Integer;
    begin
        ZipFileName := 'Picture.zip';
        datacompresion.CreateZipArchive();
        MyItem.Reset();
        MyItem.FindSet();
        repeat
            if MyItem.Picture.Count > 0 then begin
                ItemCnt := ItemCnt + 1;
                for Index := 1 to MyItem.Picture.Count do begin
                    PicCount := PicCount + 1;
                    if TenantMedia.Get(MyItem.Picture.Item(Index)) then begin
                        TenantMedia.calcfields(Content);
                        if TenantMedia.Content.HasValue then begin
                            TenantMedia.Content.CreateInStream(PicInstream);
                            datacompresion.AddEntry(PicInStream, MyItem."No." + '.png');
                        end;
                    end;
                end;
            end;
        until MyItem.Next() 0;
        Message('Items processed ' + Format(ItemCnt' Pictures processed ' + Format(PicCount));
        blobStorage.CreateOutStream(ZipOutStream);
        datacompresion.SaveZipArchive(ZipOutStream);
        datacompresion.CloseZipArchive();
        blobStorage.CreateInStream(ZipInStream);
        DownloadFromStream(ZipInStream, 'Download zip file''''', ZipFileName);
    end;

This code simple create the steam of image media and add in zip archive

at last we download the full image file zip at once using DownloadFromSteam command


No comments:

Post a Comment