我想点击下载按钮下载ZIP文件 [英] I want to download ZIP file whenver click download button

查看:181
本文介绍了我想点击下载按钮下载ZIP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你点击下载按钮,然后自动下载zip文件,它下载文件显示

类型 - File.files存储在数据库中。



if u click on download button then automatically download zip file bt it download file showing
Type - File.files are stored on database.

private DataTable GetData(SqlCommand cmd)
   {

       DataTable dt = new DataTable();
       SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["fuzzyconnection"]);
       SqlDataAdapter sda = new SqlDataAdapter();
       cmd.CommandType = CommandType.Text;
       cmd.Connection = con;
       try
       {
           con.Open();
           sda.SelectCommand = cmd;
           sda.Fill(dt);
           return dt;
       }
       catch
       {
           return null;
       }

       finally
       {
           con.Close();
           sda.Dispose();
           con.Dispose();
       }
   }
   private void download(DataTable dt)
   {
       if (dt.Rows.Count > 0)
       {
           Byte[] bytes = (Byte[])dt.Rows[0]["files"];
           Response.Buffer =true;
           //Response.Charset = "";
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           //Response.ContentType = dt.Rows[0]["Ftype"].ToString();
           //take file into some var
           //zip the file using C# code
           //zip file u use below

           Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["filenam"].ToString());
           //Response.BinaryWrite("<script type='text/javascript'> <embed src='bytes' style=width:300px; height:200px;> </embed> </script> ");
           Response.ContentType = "application/zip";

           Response.BinaryWrite(bytes);
           Response.Flush();
           Response.End();
       }
   }

推荐答案

using Ionic.Zip;
using Ionic.Zlib;







Response.ContentType = "application/zip";
       string filename = "ZipFile";


       Response.AddHeader("content-disposition", "attachment; filename=" +filename );
       using (ZipFile zipfile = new ZipFile())
       {
           zipfile.AddSelectedFiles("*.*",path,true);
           zipfile.Save(Response.OutputStream);
       }


这篇关于我想点击下载按钮下载ZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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