C# 文件操作(上傳 下載 刪除 文件列表...)

using System.IO;

1.文件上傳
----------
如下要點:
HTML部分:
  1. <form id="form1" runat="server" method="post" enctype="multipart/form-data">
  2. <input id="FileUpLoad" type="file" runat="server"/><br />
後臺CS部分 按鈕事件 :
  1. string strFileFullName = System.IO.Path.GetFileName(this.FileUpLoad.PostedFile.FileName);
  2. this.FileUpLoad.PostedFile.SaveAs(Server.MapPath("./xmlzip/") + trFileFullName);
2.文件下載
----------
ListBox的SelectedIndexChanged事件 設定相關下載連接
    protected void lst_DownLoadFileList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string strJS = "window.open('xmlzip/";
            strJS += this.lst_DownLoadFileList.SelectedItem.Text.Trim();
            strJS += "'); return false; ";
            this.imgbtn_DownLoadFile.Attributes.Add("onclick", strJS);
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
   }
或者也可以通過 改變Label的Text值 來實現點擊後實現文件下載的超級連接
this.Label1.Text = "<a href=/"xmlzip/a.rar/">a.rar</a>"

3.文件刪除
---------
string strFilePath = Server.MapPath("../CountryFlowMgr/xmlzip/"+this.lst_DownLoadFileList.SelectedItem.Text.Trim());
if (File.Exists(strFilePath))
{
   File.Delete(strFilePath);
   if (File.Exists(strFilePath))
   {
 Response.Write("ok");
   }
   else
   {
        Response.Write("ok");
   }
}


4.得到文件夾下的文件列表
-----------
#region 得到當前可用的文件列表
    /// <summary>
    /// 得到當前可用的文件列表
    /// </summary>
    /// <param name="IsAlert">是否需要彈出提示信息</param>
    private void fn_getCurrFileList(bool IsAlert)
    {
        try
        {
            //查找xmlzip文件夾下 屬於其本身UnitCoding的相關zip文件
            string strXmlZipDirectory = Server.MapPath("../xmlzip/");
            if (Directory.Exists(strXmlZipDirectory))
            {























發佈了12 篇原創文章 · 獲贊 13 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章