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...
Answered

Data compression Codeunit 425

(0) ShareShare
ReportReport
Posted on by 574

I am trying to download all item pictures. I am using new codeunit 425 data compression but I cannot make it work.

Seems like it creates correctly zip object but I cant download it (it downloads not the zip file but the last picture). Is there any documentation (apart from Microsoft github) that explain or has examples of the methods of this codeunit.

My code is:

codeunit 50999 ItemPictureBulkExtract
{
    trigger OnRun()
    var
        PicInStream: InStream;
        Index: Integer;
        TenantMedia: Record "Tenant Media";
        FileName: Text;
        datacompresion: Codeunit "Data Compression";
        ZipFileName: Text;
        ZipOutStream: OutStream;
        ZipInStream: InStream;
        blobStorage: Codeunit "Temp Blob";
    begin
        datacompresion.CreateZipArchive();
        repeat
            with item do begin
                if Picture.Count > 0 then begin
                    for Index := 1 to Picture.Count do begin
                        if TenantMedia.Get(Picture.Item(Index)) then begin
                            TenantMedia.calcfields(Content);
                            if TenantMedia.Content.HasValue then begin
                                FileName := "No." + '-' + format(Index+ GetTenantMediaFileExtension(TenantMedia);
                                TenantMedia.Content.CreateInStream(PicInstream);
                                datacompresion.AddEntry(PicInStream, FileName);
                            end;
                        end;
                    end;
                end;

            end;
        until Item.Next() 0;
        blobStorage.CreateOutStream(ZipOutStream);
        datacompresion.SaveZipArchive(ZipOutStream);
        datacompresion.CloseZipArchive();
        blobStorage.CreateInStream(ZipInStream);
        DownloadFromStream(ZipInStream, '''''', FileName)
    end;


}
I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    The code seems ok. I'm using the same code for bulk exporting all the item pictures I have on my database and it works.

    The only strange things I saw here:

    DownloadFromStream(ZipInStream, '', '', '', FileName) --> why are you using FileName? Instead, you should use the ZIP file name variable (ZipFileName) othewrise you have a file (ZIP) that is called like your last image file.

  • AntonioTorre Profile Picture
    574 on at

    Stefano, there is a saying in Spain that says: "The trees dont let you see the forest"....

    You are right: the code was working fine, but at the very end, as I was getting a file called picxxxx.jpg and I did not open it I did not realize that it was in fact the zip file but with that wrong name.

    Thank you, it is working now!

  • Community Member Profile Picture
    on at

    Hi,

    I have written same code as above and also corrected below line. No image or zipfile is downloaded.

    DownloadFromStream(ZipInStream, '''''', ZipFileName).

    Below is the complete code                

    if ItemCode = '' then
                error('Add item filter');
            MyItem.SetFilter("No.", '%1', ItemCode);
            ZipFileName := 'Picture.zip';
            datacompresion.CreateZipArchive();
            repeat
                with MyItem do begin
                    if Picture.Count > 0 then begin
                        ItemCnt := ItemCnt + 1;
                        for Index := 1 to Picture.Count do begin
                            PicCount := PicCount + 1;
                            FileName := '';
                            if TenantMedia.Get(Picture.Item(Index)) then begin
                                TenantMedia.calcfields(Content);
                                if TenantMedia.Content.HasValue then begin
                                    FileName := "No." + '-' + format(Index+ GetTenantMediaFileExtension(TenantMedia);
                                    TenantMedia.Content.CreateInStream(PicInstream);
                                    datacompresion.AddEntry(PicInStream, FileName);
                                    
                                end;
                            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);
    Not sure what is wrong with this code.

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 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans