Create and stream image archive zip file for download C#

前端 未结 1 1705
广开言路
广开言路 2021-02-11 07:43

I am using the beloved DotNetZip archiving library in MVC3 to generate a Zip file on the fly which contains .png images from binaries stored in a database. I then stream the gen

相关标签:
1条回答
  • 2021-02-11 08:10

    Your calls to stream.Seek(0, SeekOrigin.Begin) are causing the contents of the stream to be overwritten each iteration with the most recent image data. Try this instead:

    using (ZipFile zipFile = new ZipFile())
    {
        foreach (var userPicture in userPictures)
        {
            string pictureName = userPicture.Name + ".png";
            using (MemoryStream tempstream = new MemoryStream())
            {
                Image userImage = //method that returns Drawing.Image from byte[];   
                userImage.Save(tempstream, ImageFormat.Png);  
                tempstream.Seek(0, SeekOrigin.Begin);
                byte[] imageData = new byte[tempstream.Length];
                tempstream.Read(imageData, 0, imageData.Length);
                zipFile.AddEntry(pictureName, imageData);
            }
        }
    
        zipFile.Save(Response.OutputStream);
    }
    
    0 讨论(0)
提交回复
热议问题