Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8579571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:38:09+00:00 2026-06-11T20:38:09+00:00

I am using the beloved DotNetZip archiving library in MVC3 to generate a Zip

  • 0

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 generated Zip file out for the user to download. (I validate image data prior to saving to to the database, so you can assume that all image data is valid).

public ActionResult PictureExport()
      {
           IEnumerable<UserPicture> userPictures = db.UserPicture.ToList();
           //"db" is a DataContext and UserPicture is the model used for uploaded pictures.
           DateTime today = DateTime.Now;
           string fileName = "attachment;filename=AllUploadedPicturesAsOf:" + today.ToString() + ".zip";
           this.Response.Clear();
           this.Response.ContentType = "application/zip";
           this.Response.AddHeader("Content-Disposition", fileName);

           using (ZipFile zipFile = new ZipFile())
             {
               using (MemoryStream stream = new MemoryStream())
                {
                 foreach (UserPicture userPicture in userPictures)
                  {
                     stream.Seek(0, SeekOrigin.Begin);
                     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);
                        stream.Seek(0, SeekOrigin.Begin);
                        tempstream.WriteTo(stream);
                     }

                     zipFile.AddEntry(pictureName, stream);
                 }

                zipFile.Save(Response.OutputStream);
              }

           }

        this.Response.End();
        return RedirectToAction("Home");
      }

This code will work for uploading and exporting ONE (1) image. However, after uploading more than one image to the database and then attempting to export them all, the Zip file that is generated will only contain the data of the most recent uploaded image. All the other image NAMES will appear in the zip file, but their file size will be 0 and they are simply empty files.

I’m guessing my issue has to do with the MemoryStreams (or that I’m missing something simple), but as far as I can tell by stepping through the code, the images are being pulled from the database and are being added to the zip file successfully…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:38:10+00:00Added an answer on June 11, 2026 at 8:38 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using Java,I have to fetch multiple sets of values from an XML file to
Using CMake I want to check if a particular function (cv::getGaborKernel) from OpenCV library
Using shell script I want to read a word from text file and return
We are using the beloved Ninject+Ninject.Web.Mvc with MVC 2 and are running into some
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using the HTML5 File API I can get the Binary String representation of a
Using mercurial, I've run into an odd problem where a line from one committer
Using MVC2 I have an AJAX form which is posting to a bound model.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.