简体   繁体   English

创建并流式传输图像存档zip文件以下载C#

[英]Create and stream image archive zip file for download C#

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. 我正在使用MVC3中最受欢迎的DotNetZip归档库来动态生成一个Zip文件,其中包含来自数据库中存储的二进制文件的.png图像。 I then stream the generated Zip file out for the user to download. 然后,我将生成的Zip文件流化出来供用户下载。 (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. 此代码适用于上载和导出一(1)张图像。 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. 但是,在将一个以上的图像上传到数据库,然后尝试全部导出之后,生成的Zip文件将仅包含最新上传的图像的数据。 All the other image NAMES will appear in the zip file, but their file size will be 0 and they are simply empty files. 所有其他图像“ NAMES”将显示在zip文件中,但它们的文件大小将为0,并且它们只是空文件。

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... 我猜我的问题与MemoryStreams有关(或者我缺少一些简单的东西),但是据我所知,通过逐步执行代码,图像已从数据库中提取并添加到了压缩文件成功...

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. 您对stream.Seek(0,SeekOrigin.Begin)的调用导致每次迭代都使用最新的图像数据覆盖流的内容。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM