Hi,
This is my table on MySQL database.
+----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
| 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 |
+----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
On gridview using asp net and c# I have set
<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>
I need download a single zip file that joins the individual four files.
I have tried this tutorial but the zip file is empty.
If change this part of code from
string img = reader["img"].ToString();
string ppt = reader["ppt"].ToString();
string xls = reader["xls"].ToString();
string pdf = reader["pdf"].ToString();
to
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";
the files are downloaded correctly.
My code below
Please help me do it.
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();
}
}
}
}