使用C#和VB.NET在ASP.NET中将多个文件作为zip存档文件下载 [英] Download multiple files as zip archive file in ASP.NET using C# and VB.NET

查看:74
本文介绍了使用C#和VB.NET在ASP.NET中将多个文件作为zip存档文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单点击下载按钮,它将下载所有文件,不需要复选框选择。如果省略chcekbox这里是demo 使用C#和VB.Net在ASP.Net中下载多个文件作为Zip存档文件 [ ^ ]



我尝试过:



页面加载代码

i want to simply click download button and it will download all the files no checkbox selection is required.how to omit chcekbox here is the link of demo Download multiple files as Zip Archive File in ASP.Net using C# and VB.Net[^]

What I have tried:

page load code

if (!IsPostBack)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
            List<listitem> files = new List<listitem>();
            foreach (string filePath in filePaths)
            {
                files.Add(new ListItem(Path.GetFileName(filePath), filePath));
            }
            GridView1.DataSource = files;
            GridView1.DataBind();
        }

button click code

  using (ZipFile zip = new ZipFile())
        {
            zip.AlternateEncodingUsage = ZipOption.AsNecessary;
            zip.AddDirectoryByName("Files");
            foreach (GridViewRow row in GridView1.Rows)
            {
                if ((row.FindControl("chkSelect") as CheckBox).Checked)
                {
                    string filePath = (row.FindControl("lblFilePath") as Label).Text;
                    zip.AddFile(filePath, "Files");
                }
            }
        
            Response.Clear();
            Response.BufferOutput = false;
            string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=" + zipName);  
            zip.Save(Response.OutputStream);
            Response.End();
        }

推荐答案

从标记中删除此行

Remove this line from markup
<asp:CheckBox ID="chkSelect" runat="server" />




代码背后的




in Code behind:

foreach (GridViewRow row in GridView1.Rows)
          {
              if ((row.FindControl("chkSelect") as CheckBox).Checked) // remove this line
              {
                  string filePath = (row.FindControl("lblFilePath") as Label).Text;
                  zip.AddFile(filePath, "Files");
              }
          }


这篇关于使用C#和VB.NET在ASP.NET中将多个文件作为zip存档文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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