Default2.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:6k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. public partial class web_pos_c_Default2 : System.Web.UI.Page
  13. {
  14.     string StrFilePath = "~/web/oa_upload";
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         int flag = 0;
  18.         Button3.Attributes.Add("onclick","return confirm('確定要刪除該文件嗎?')");
  19.         string filePath = Server.MapPath(StrFilePath);
  20.         if (Directory.Exists(filePath))
  21.         {
  22.             DirectoryInfo[] arrfile = new DirectoryInfo(filePath).GetDirectories(); 
  23.             foreach (DirectoryInfo s in arrfile)
  24.             {
  25.                 flag = 0;
  26.                 for (int i = 0; i < DropDownList1.Items.Count;i++ )
  27.                 {
  28.                     if (s.Name.Equals(Convert.ToString(DropDownList1.Items[i].Text)))
  29.                     {
  30.                         flag = flag + 1;
  31.                     }
  32.                 }
  33.                 if (flag == 0)
  34.                 {
  35.                     DropDownList1.Items.Add(s.Name);
  36.                 }
  37.             }
  38.         }
  39.         string str = StrFilePath + "/" + DropDownList1.Text;
  40.         InitFolderInfo(str);
  41.     }
  42.     private void InitFolderInfo(string s)
  43.     {
  44.         int count = 0;
  45.         //将虚拟路径转换为物理路径
  46.         string strFilePath = Server.MapPath(s);
  47.         //声明文件夹已经使用的容量
  48.         double iFolderCurrentSize = 0;
  49.         //获取文件夹中的所有文件
  50.         FileInfo[] arrFiles = new DirectoryInfo(strFilePath).GetFiles();
  51.         //循环文件获已经使用的容量
  52.         foreach (FileInfo fi in arrFiles)
  53.         {
  54.             iFolderCurrentSize += Convert.ToInt32(fi.Length / 1024);
  55.             count = count + 1;
  56.         }
  57.         //把文件夹容量和以用文件夹容量赋值给标签
  58.         lbl_FolderInfo.Text = string.Format("文件夹文件数目:{0}个,已用容量:{1}KB", count, iFolderCurrentSize);
  59.     }
  60.     protected void ShowMessageBox(string strMessage)
  61.     {
  62.         Response.Write(string.Format("<script>alert('{0}')</script>", strMessage));
  63.     }
  64.     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  65.     {
  66.         TextBox1.Text = "";
  67.         ListBox1.Items.Clear();
  68.         string str = StrFilePath + "/" + DropDownList1.Text;
  69.         string filePath = Server.MapPath(StrFilePath + "/" + DropDownList1.Text);
  70.         if (Directory.Exists(filePath))
  71.         {
  72.             FileInfo[] arrfile = new DirectoryInfo(filePath).GetFiles();
  73.             foreach (FileInfo s in arrfile)
  74.             {
  75.                 ListBox1.Items.Add(s.Name);
  76.             }
  77.         }
  78.         InitFolderInfo(str);
  79.     }
  80.     protected void Button1_Click(object sender, EventArgs e)
  81.     {
  82.         string filepath = StrFilePath + "/" + DropDownList1.Text;
  83.         string filename = ListBox1.SelectedValue;
  84.         string fullfilename = Server.MapPath(filepath + "/") + filename;
  85.         FileInfo DowloadFile = new FileInfo(fullfilename);
  86.         Response.Clear();
  87.         Response.ClearHeaders();
  88.         Response.Buffer = false;
  89.         Response.ContentType = "application/octet-stream";
  90.         Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DowloadFile.FullName, System.Text.Encoding.UTF8));
  91.         Response.AppendHeader("Content-Length", DowloadFile.Length.ToString());
  92.         Response.WriteFile(DowloadFile.FullName);
  93.         Response.Flush();
  94.         Response.End();
  95.     }
  96.     protected void Button3_Click(object sender, EventArgs e)
  97.     {
  98.         string filepath = StrFilePath + "/" + DropDownList1.Text;
  99.         string filename = ListBox1.SelectedValue;
  100.         string strFilePhysicalPath = Server.MapPath(filepath + "/") + filename;
  101.         File.Delete(strFilePhysicalPath);
  102.         ListBox1.Items.Remove(ListBox1.Items.FindByText(filename));
  103.         InitFolderInfo(filepath);
  104.         TextBox1.Text = "";
  105.         ShowMessageBox("删除成功!");
  106.     }
  107.     protected void Button2_Click(object sender, EventArgs e)
  108.     {
  109.         string filepath = StrFilePath + "/" + DropDownList1.Text;
  110.         string filename = ListBox1.SelectedValue;
  111.         string filenewname = TextBox1.Text;
  112.         string FilePhysicalPath = Server.MapPath(filepath + "/") + filename;
  113.         string FileNewPhysicalPath = Server.MapPath(filepath + "/") + filenewname;
  114.         File.Move(FilePhysicalPath, FileNewPhysicalPath);
  115.         ListItem li = ListBox1.Items.FindByText(filename);
  116.         li.Text = filenewname;
  117.         li.Value = filenewname;
  118.         ShowMessageBox("文件覆盖成功!");
  119.     }
  120.     #region 列表框事件 ListBox1_SelectedIndexChanged()
  121.     protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
  122.     {
  123.         if (!DropDownList1.Text.Equals(""))
  124.         {
  125.             string filepath = StrFilePath + "/" + DropDownList1.Text;
  126.             string filename = ListBox1.SelectedValue;
  127.             string FilePhysicalPath = Server.MapPath(filepath + "/") + filename;
  128.             //根据物理路径实例化文件信息类
  129.             if (!filename.Equals(""))
  130.             {
  131.                 FileInfo fi = new FileInfo(FilePhysicalPath);
  132.                 //或得文件大小和创建日期赋值给标签
  133.                 lbl_FileDescription.Text = string.Format("文件大小:{0}字节<br><br>上传时间:{1}<br>", fi.Length, fi.CreationTime);
  134.                 //把文件名赋值给重命名文件框
  135.                 TextBox1.Text = filename;
  136.             }
  137.         }
  138.     }
  139.     #endregion
  140. }