正在压缩文件和下载他们使用另存为对话框 [英] Zipping Files and Downloading them Using SaveAs Dialog

查看:133
本文介绍了正在压缩文件和下载他们使用另存为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code来压缩所有文件,然后将其保存到硬盘。我想压缩所有文件(这样做),然后将zip文件附加到响应流,使用户必须保存它的选项!

 保护无效DownloadSelectedFiles_Click(对象发件人,EventArgs的发送)
        {
            路径字符串=的String.Empty;
            字符串zipFilePath = @C:\\ MyZipFile.zip            ZipOutputStream zipStream =新ZipOutputStream(File.Create(zipFilePath));
            字节[]缓冲区=新的字节[4096];            的foreach(在gvFiles.Rows GridViewRow行)
            {
                布尔isSelected =(row.FindControl(chkSelect)作为复选框).Checked;                如果(isSelected)
                {
                    路径=(row.FindControl(lblUrl)作为标签)。文本;                    入门的ZipEntry =新的ZipEntry(Path.GetFileName(路径));
                    entry.DateTime = DateTime.Now;                    zipStream.PutNextEntry(输入);                    使用(的FileStream FS = File.OpenRead(路径))
                    {
                        INT sourceBytes;
                        做
                        {
                            sourceBytes = fs.Read(缓冲液,0,buffer.Length);
                            zipStream.Write(缓冲液,0,sourceBytes);
                        }而(sourceBytes大于0);
                    }
                }
            }
            zipStream.Finish();
            zipStream.Close();            Response.ContentType =应用程序/ x-ZIP-COM pressed
            Response.AppendHeader(内容处置,附件;文件名= SS.zip);
            Response.WriteFile(zipFilePath);
            到Response.End();        }


解决方案

我的博客上讲述前一阵子发送文件是这样的。您可能会发现有用的东西在里面。

http://absolutecobblers.blogspot.com/2008/02/downloading-and-deleting-temporary.html

I have the following code to zip all the files and then save it to the harddisk. I want zip all the files (this is done) and then attach the zip file to the Response stream so that the user have the option to save it!

protected void DownloadSelectedFiles_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            string zipFilePath = @"C:\MyZipFile.zip";

            ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
            byte[] buffer = new byte[4096];

            foreach (GridViewRow row in gvFiles.Rows)
            {
                bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;

                if (isSelected)
                {
                    path = (row.FindControl("lblUrl") as Label).Text;

                    ZipEntry entry = new ZipEntry(Path.GetFileName(path));
                    entry.DateTime = DateTime.Now;

                    zipStream.PutNextEntry(entry);

                    using (FileStream fs = File.OpenRead(path))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
            }


            zipStream.Finish();
            zipStream.Close();

            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
            Response.WriteFile(zipFilePath);
            Response.End();

        }

解决方案

I blogged about sending files like this a while ago. You might find something usefull in there.

http://absolutecobblers.blogspot.com/2008/02/downloading-and-deleting-temporary.html

这篇关于正在压缩文件和下载他们使用另存为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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