1

I've got this code snipped that download successfully:

byte[] bytes;
bytes = Convert.FromBase64String(lehrling.passfoto);
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.AddHeader("content-disposition", "attachment; filename=" + lehrling.vorname + "." + lehrling.nachname + ".jpeg");
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.End()

This works just fine. I get a jpeg file out of it. Now:

I created a for-loop that executes the code shown above for each bytearray:

    for (int i = 0; i < anzahlBilder; i++)
    {
         //My Code here
    }
    Response.End()

I get anzahlBilder from somewhere else. its not important for my question. I put Response.End() outside of my for-loop because otherwise it ends after 1 image got downloaded.

To Zip:

Now i want to create a .Zip file, which contains all my images. I dont know how to do it. Any Suggestions?

1 Answers1

0

By using System.IO.Compression.ZipFile

You need to add a dll reference to the assembly, "System.IO.Compression.FileSystem.dll" and import namespace.

System.IO.Compression

at the top.

    ZipFile zipFile = new ZipFile();
    for(int i = 0; i < anzahlBilder; i++)
    {
        using (MemoryStream ms= new MemoryStream(Convert.FromBase64String(lehrling.passfoto)))
        {
            Image userImage = Image.FromStream(ms);   
            userImage.Save(ms, ImageFormat.Jpeg);  
            ms.Seek(0, SeekOrigin.Begin);
            byte[] imageData = new byte[ms.Length];
            ms.Read(imageData, 0, imageData.Length);
            zipFile.AddEntry(lehrling.vorname + "." + lehrling.nachname + ".jpeg", imageData);
        }
    }

    zipFile.Save(Response.OutputStream);

make required changes in place of lehrling.passfoto and lehrling.vorname + "." + lehrling.nachname + ".jpeg" to get it work.

Imad
  • 7,126
  • 12
  • 55
  • 112