微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将多个文件下载为 Zip 文件返回空的 Zip 文件

如何解决将多个文件下载为 Zip 文件返回空的 Zip 文件

这是我在 MysqL 数据库上的表。

+----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
| ID | xls                              | img                             | ppt                              | pdf                             |
+----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
|  1 | c:\inetpub\wwwroot\xls\test.xlsx | c:\inetpub\wwwroot\img\test.jpg | c:\inetpub\wwwroot\ppt\test.pptx | c:\inetpub\wwwroot\pdf\test.pdf |
+----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+

在使用 asp net 和 c# 的 gridview 上我已经设置

<asp:TemplateField
    ItemStyle-HorizontalAlign="Center"
    HeaderText="Download">
    <ItemTemplate>
        <asp:ImageButton ID="img" runat="server"
            ImageUrl="/aspnet/img/zip.gif" OnClick="img_Click" />
    </ItemTemplate>
</asp:TemplateField>

我需要下载一个单独的 zip 文件来连接这四个文件

我试过这个tutorial

但是 zip 文件是空的。

如果将这部分代码

string img = reader["img"].ToString();
string ppt = reader["ppt"].ToString();
string xls = reader["xls"].ToString();
string pdf = reader["pdf"].ToString();

string img = @"c:\inetpub\wwwroot\img\test.jpg";
string ppt = @"c:\inetpub\wwwroot\ppt\test.pptx";
string xls = @"c:\inetpub\wwwroot\xls\test.xlsx";
string pdf = @"c:\inetpub\wwwroot\pdf\test.pdf";

文件下载正确。

我的代码如下

请帮我做。

using (MysqLConnection cn =
    new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
    using (MysqLCommand cmd =
        new MysqLCommand("SP_zip",cn))
    {
        cmd.Connection.open();
        cmd.CommandType = CommandType.StoredProcedure;

        MysqLDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {                  
            using (ZipFile zip = new ZipFile())
            {
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AddDirectoryByName("Files");

                string img = reader["img"].ToString();
                string ppt = reader["ppt"].ToString();
                string xls = reader["xls"].ToString();
                string pdf = reader["pdf"].ToString();

                if (img != string.Empty)
                {
                    zip.AddFile(img,"Files");
                }
                if (ppt != string.Empty)
                {
                    zip.AddFile(ppt,"Files");
                }
                if (xls != string.Empty)
                {
                    zip.AddFile(xls,"Files");
                }
                if (pdf != string.Empty)
                {
                    zip.AddFile(pdf,"Files");
                }

                Response.Clear();
                Response.BufferOutput = false;
                string zipName = String.Format("Zip_{0}.zip",DateTime.Now.ToString("yyyyMMMddHHmmss"));
                Response.ContentType = "application/zip";
                response.addheader("content-disposition","attachment; filename=" + zipName);
                zip.Save(Response.OutputStream);
                Response.End();
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。