[C#] 文件下载 →→→→→进入此内容的聊天室

来自 , 2019-08-13, 写在 C#, 查看 106 次.
URL http://www.code666.cn/view/8e68c3c7
  1. //文件下载
  2.     protected void FileAction(object source, RepeaterCommandEventArgs e)
  3.     {
  4.         switch (e.CommandName)
  5.         {
  6.             case "DownTemp":
  7.                 int downID = Utility.getVal(e.CommandArgument);
  8.                 DataSet ds = Common.NoPager2("Std_BZFiles", "", "ID,FileName,FileUrl", "", "ID='" + downID + "'");
  9.  
  10.                 string strFilePhysicalPath = ds.Tables[0].Rows[0]["FileUrl"].ToString();
  11.                 int start = strFilePhysicalPath.LastIndexOf(".") + 1;
  12.                 int total = strFilePhysicalPath.Length - strFilePhysicalPath.LastIndexOf(".") - 1;
  13.                 string FileUrl = ds.Tables[0].Rows[0]["FileUrl"].ToString();
  14.  
  15.                 int k = FileUrl.LastIndexOf("\\");
  16.                 string strFileName = FileUrl.Substring(k + 37);
  17.                 //清空输出流
  18.                 Response.Clear();
  19.  
  20.                 //在HTTP头中加入文件名信息
  21.                 Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
  22.                 //定义输出流翸IME类型为
  23.                 Response.ContentType = "application/octet-stream";
  24.                 //从磁盘读取文件流
  25.                 System.IO.FileStream fs = System.IO.File.OpenRead(strFilePhysicalPath);
  26.                 //定义缓冲区大洙小
  27.                 byte[] buffer = new byte[102400];
  28.                 //第台?一?次?读á取?
  29.                 int i = fs.Read(buffer, 0, buffer.Length);
  30.                 //如果读取的字节大于,则使用BinaryWrite()不断向客户端输出文件流
  31.                 while (i > 0)
  32.                 {
  33.                     Response.BinaryWrite(buffer);
  34.                     i = fs.Read(buffer, 0, buffer.Length);
  35.                 }
  36.                 //关闭磁盘文件流
  37.                 fs.Close();
  38.                 //关闭输出流
  39.                 Response.End();
  40.                 break;
  41.  
  42.             default:
  43.                 break;
  44.         }
  45.     }
  46.  

回复 "文件下载"

这儿你可以回复上面这条便签

captcha