zoukankan      html  css  js  c++  java
  • asp.net文件下载

     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         }
  • 相关阅读:
    三维dem
    geoserver 通过代码实现发布地图服务
    restful发布服务
    php防攻击
    redis主从复制
    redis虚拟内存
    redis 持久化
    lamp源码安装
    mysql优化
    php匿名函数与闭包函数
  • 原文地址:https://www.cnblogs.com/keyyang/p/3810042.html
Copyright © 2011-2022 走看看