创建和流下载C#图像压缩zip文件 [英] Create and stream image archive zip file for download C#

查看:172
本文介绍了创建和流下载C#图像压缩zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3心爱DotNetZip归档库生成包含巴纽从存储在数据库中的二进制图像飞Zip文件。然后,我流生成的Zip文件出用户下载。 (我要保存到数据库之前验证的图像数据,所以你可以假定所有的图像数据是有效的)。

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");
      }

这code将上传和导出一(1)影像工作。然而,上传多个图像到数据库,然后尝试导出所有这些之后,生成的Zip文件将只包含最新上传图像的数据。所有其他图像名称将出现在zip文件,但他们的文件大小为0,他们只是空文件。

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.

我猜我的问题已经做的MemoryStreams(或者说我失去了一些东西简单),但是就通过了code踩着我所知道的,图像正从数据库中抽取和被添加到压缩文件...成功

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

推荐答案

您到stream.Seek(0,SeekOrigin.Begin)调用导致流的内容将被重写每次迭代与最近的图像数据。试试这个:

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);
}

这篇关于创建和流下载C#图像压缩zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆