码迷,mamicode.com
首页 > Web开发 > 详细

asp.net文件下载

时间:2014-06-27 17:02:31      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

 1       protected void btn_Down(object sender, EventArgs e)
 2         {
 3             string filePath = Server.MapPath("/TradeLog/" + txtDate.Text.Trim());
 4             if (!downBeforeValidation(filePath))
 5                 return;
 6             dlZipDir(filePath, txtDate.Text);
 7         }    
 8 
 9        /// <summary>
10         /// 下载前验证
11         /// </summary>
12         /// <param name="filePath">文件路径</param>
13         /// <returns></returns>
14         private bool downBeforeValidation(string filePath)
15         {
16             //判断密码是否正确
17             string downPassWord = WebConfigurationManager.AppSettings["downPassWord"];
18             if (string.IsNullOrEmpty(txtPassWord.Text) || txtPassWord.Text.Trim() != downPassWord)
19             {
20                 Response.Write("<script>alert(‘密码错误或为空‘)</script>");
21                 return false;
22             }
23             //判断文件是否存在
24 
25             if (!Directory.Exists(filePath))
26             {
27                 Response.Write("<script>alert(‘目录不存在‘)</script>");
28                 return false;
29             }
30             return true;
31         }
32 
33       /// <summary>
34         /// 全部变量
35         /// </summary>
36         ZipOutputStream zos = null;
37         String strBaseDir = "";
38         /// <summary>
39         /// 下载文件
40         /// </summary>
41         /// <param name="strPath">路径</param>
42         /// <param name="strFileName">下载后默认显示文件名称</param>
43         void dlZipDir(string strPath, string strFileName)
44         {
45             MemoryStream ms = null;
46             Response.ContentType = "application/octet-stream";
47             strFileName = HttpUtility.UrlEncode(strFileName).Replace(+,  );
48             Response.AddHeader("Content-Disposition", "attachment;   filename=" + strFileName + ".zip");
49             ms = new MemoryStream();
50             zos = new ZipOutputStream(ms);
51             strBaseDir = strPath + "\\";
52             addZipEntry(strBaseDir);
53             zos.Finish();
54             zos.Close();
55             Response.Clear();
56             Response.BinaryWrite(ms.ToArray());
57             Response.End();
58         }
59 
60      /// <summary>
61         /// 生成压缩文件
62         /// </summary>
63         /// <param name="PathStr"></param>
64         void addZipEntry(string PathStr)
65         {
66             DirectoryInfo di = new DirectoryInfo(PathStr);
67             foreach (DirectoryInfo item in di.GetDirectories())
68             {
69                 addZipEntry(item.FullName);
70             }
71             foreach (FileInfo item in di.GetFiles())
72             {
73                 FileStream fs = File.OpenRead(item.FullName);
74                 byte[] buffer = new byte[fs.Length];
75                 fs.Read(buffer, 0, buffer.Length);
76                 string strEntryName = item.FullName.Replace(strBaseDir, "");
77                 ZipEntry entry = new ZipEntry(strEntryName);
78                 zos.PutNextEntry(entry);
79                 zos.Write(buffer, 0, buffer.Length);
80                 fs.Close();
81             }
82         }

 

asp.net文件下载,布布扣,bubuko.com

asp.net文件下载

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/keyyang/p/3810042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!