自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#文件上傳下載及列表相關(guān)代碼示例

開發(fā) 后端
這里將介紹C#文件上傳下載及列表相關(guān)代碼示例,這些示例將幫助大家更好的理解C#的功能機制,希望對大家有所幫助。

C#文件上傳下載是對文件的基本操作,C#文件上傳下載主要實現(xiàn)的是對文件的管理與應(yīng)用。這里介紹的代碼將實現(xiàn)大部分功能。

1.文件上傳

如下要點:

HTML部分:

  1. <form id=\"form1\" runat=\"server\" method=\"post\" enctype=\"multipart/form-data\"> 
  2.     <input id=\"FileUpLoad\" type=\"file\" runat=\"server\"/><br /> 
  3.     后臺CS部分 按鈕事件  
  4.     //string strFileFullName = System.IO.Path.GetFileName(this.FileUpLoad.PostedFile.FileName);  
  5.     //this.FileUpLoad.PostedFile.SaveAs(Server.MapPath(\"./Xmlzip/\") + strFileFullName); 

2.文件下載

ListBox的SelectedIndexChanged事件 設(shè)定相關(guān)下載連接    

  1. protected void lst_DownLoadFileList_SelectedIndexChanged(object sender, EventArgs e)  
  2.         {  
  3.             try 
  4.             {  
  5.                 string strJS = \"window.open(\'Xmlzip/\";  
  6.                 strJS += this.lst_DownLoadFileList.SelectedItem.Text.Trim();  
  7.                 strJS += \"\'); return false; \";  
  8.                 this.imgbtn_DownLoadFile.Attributes.Add(\"onclick\", strJS);  
  9.             }  
  10.             catch (Exception ex)  
  11.             {  
  12.                 ex.ToString();  
  13.             }  
  14.         } 

或者也可以通過 改變Label的Text值 來實現(xiàn)點擊后實現(xiàn)文件下載的超級連接

this.Label1.Text = \"<a href=\\\"Xmlzip/a.rar\\\">a.rar</a>\"

3.文件刪除

  1. string strFilePath = Server.MapPath(\"../CountryFlowMgr/Xmlzip/\"+this.lst_DownLoadFileList.SelectedItem.Text.Trim());  
  2.     if (File.Exists(strFilePath))  
  3.     {  
  4.        File.Delete(strFilePath);  
  5.        if (File.Exists(strFilePath))  
  6.        {  
  7.      Response.Write(\"ok\");  
  8.        }  
  9.        else 
  10.        {  
  11.             Response.Write(\"ok\");  
  12.        }  
  13.     } 

4.得到文件夾下的文件列表

  1. #region 得到當(dāng)前可用的文件列表  
  2.         /// <summary>  
  3.         /// 得到當(dāng)前可用的文件列表  
  4.         /// </summary>  
  5.         /// <param name=\"IsAlert\">是否需要彈出提示信息</param>  
  6.         private void fn_getCurrFileList(bool IsAlert)  
  7.         {  
  8.             try 
  9.             {  
  10.                 //查找Xmlzip文件夾下 屬于其本身UnitCoding的相關(guān)zip文件  
  11.                 string strXmlZipDirectory = Server.MapPath(\"../Xmlzip/\");  
  12.                 if (Directory.Exists(strXmlZipDirectory))  
  13.                 {  
  14.                     //DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);  
  15.                     DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);  
  16.  
  17.                     FileInfo[] FI = di.GetFiles(\"*.zip\");//只查.zip文件  
  18.                     if (FI.Length > 0)  
  19.                     {  
  20.                         lst_DownLoadFileList.Items.Clear();  
  21.                         foreach (FileInfo tmpFI in FI)  
  22.                         {  
  23.                             ListItem tmpItem = new ListItem();  
  24.                             tmpItem.Text = tmpFI.Name;  
  25.                             lst_DownLoadFileList.Items.Add(tmpItem);  
  26.                         }  
  27.                         lst_DownLoadFileList.SelectedIndex = 0;  
  28.         }  
  29.                     else 
  30.                     {  
  31.                         if (IsAlert)  
  32.                         {  
  33.                             Response.write(\"查無可以下載的文件!\");  
  34.                         }  
  35.                     }  
  36.                 }  
  37.             }  
  38.             catch (Exception ex)  
  39.             {  
  40.                 ex.ToString();  
  41.             }  
  42.         }  
  43.         #endregion 

C#文件上傳下載及列表相關(guān)代碼示例就介紹到這里。

【編輯推薦】

  1. C#多態(tài)性概念及特點的解析
  2. C#取整函數(shù)實例應(yīng)用詳解
  3. C#單元測試的一個小故事
  4. C#單元測試概念及作用的淺析
  5. C#單元測試使用的必要性的淺析
責(zé)任編輯:彭凡 來源: 中國IT實驗室
相關(guān)推薦

2018-04-20 16:15:42

Koa2上傳下載

2009-07-07 13:45:04

jspsmart

2009-07-03 14:15:54

JSP SmartUp

2015-02-11 16:34:49

微信SDK

2009-08-27 15:53:30

C#中using wo

2009-07-30 13:43:58

ASP.NET中文件上

2010-03-08 11:34:45

Linux上傳下載指令

2023-09-06 08:33:30

2021-06-04 13:07:53

LinuxPythonXshell

2009-09-02 14:06:14

C#文件傳送

2011-02-21 18:11:27

vsFTPd

2009-12-30 10:15:57

Silverlight

2024-06-17 09:02:01

2023-01-13 09:37:23

2009-07-02 13:31:13

JSP組件

2009-08-13 15:18:23

C#文件上傳

2024-06-18 08:37:25

場景異步編程代碼

2024-06-24 03:00:00

2010-01-14 15:06:27

CentOS用戶

2009-12-10 16:35:08

PHP操作文章列表
點贊
收藏

51CTO技術(shù)棧公眾號