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

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

为寒亭等地区用户提供了全套网页设计制作服务,及寒亭网站建设行业解决方案。主营业务为成都做网站、成都网站建设、寒亭网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

1.文件上传

如下要点:

HTML部分:

 
 
 
  1.  id=\"form1\" runat=\"server\" method=\"post\" enctype=\"multipart/form-data\"> 
  2.      id=\"FileUpLoad\" type=\"file\" runat=\"server\"/> /> 
  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.rar\"

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.         ///   
  3.         /// 得到当前可用的文件列表  
  4.         ///   
  5.         /// 是否需要弹出提示信息  
  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#文件上传下载及列表相关代码示例就介绍到这里。

分享名称:C#文件上传下载及列表相关代码示例
标题链接:http://www.shufengxianlan.com/qtweb/news1/1351.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联