C#文件上传下载及列表相关代码示例

开发 后端
这里将介绍C#文件上传下载及列表相关代码示例,这些示例将帮助大家更好的理解C#的功能机制,希望对大家有所帮助。

C#文件上传下载是对文件的基本操作,C#文件上传下载主要实现的是对文件的管理与应用。这里介绍的代码将实现大部分功能。

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事件 设定相关下载连接    

  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值 来实现点击后实现文件下载的超级连接

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 得到当前可用的文件列表  
  2.         /// <summary>  
  3.         /// 得到当前可用的文件列表  
  4.         /// </summary>  
  5.         /// <param name=\"IsAlert\">是否需要弹出提示信息</param>  
  6.         private void fn_getCurrFileList(bool IsAlert)  
  7.         {  
  8.             try 
  9.             {  
  10.                 //查找Xmlzip文件夹下 属于其本身UnitCoding的相关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#文件上传下载及列表相关代码示例就介绍到这里。

【编辑推荐】

  1. C#多态性概念及特点的解析
  2. C#取整函数实例应用详解
  3. C#单元测试的一个小故事
  4. C#单元测试概念及作用的浅析
  5. C#单元测试使用的必要性的浅析
责任编辑:彭凡 来源: 中国IT实验室
相关推荐

2009-07-07 13:45:04

jspsmart

2018-04-20 16:15:42

Koa2上传下载

2009-07-03 14:15:54

JSP SmartUp

2015-02-11 16:34:49

微信SDK

2009-07-30 13:43:58

ASP.NET中文件上

2010-03-08 11:34:45

Linux上传下载指令

2009-08-27 15:53:30

C#中using wo

2021-06-04 13:07:53

LinuxPythonXshell

2023-09-06 08:33:30

2009-09-02 14:06:14

C#文件传送

2011-02-21 18:11:27

vsFTPd

2009-12-30 10:15:57

Silverlight

2023-01-13 09:37:23

2009-08-13 15:18:23

C#文件上传

2009-07-02 13:31:13

JSP组件

2010-01-14 15:06:27

CentOS用户

2009-12-10 16:35:08

PHP操作文章列表

2009-08-24 08:56:55

C#反射

2009-11-16 10:57:51

PHP上传文件代码

2010-03-05 11:04:00

C调用Python函数
点赞
收藏

51CTO技术栈公众号