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

为寒亭等地区用户提供了全套网页设计制作服务,及寒亭网站建设行业解决方案。主营业务为成都做网站、成都网站建设、寒亭网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
1.文件上传
如下要点:
HTML部分:
- id=\"form1\" runat=\"server\" method=\"post\" enctype=\"multipart/form-data\">
 - id=\"FileUpLoad\" type=\"file\" runat=\"server\"/>
 
/>- 后台CS部分 按钮事件
 - //string strFileFullName = System.IO.Path.GetFileName(this.FileUpLoad.PostedFile.FileName);
 - //this.FileUpLoad.PostedFile.SaveAs(Server.MapPath(\"./Xmlzip/\") + strFileFullName);
 
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.rar\"
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 得到当前可用的文件列表
 - ///
 - /// 得到当前可用的文件列表
 - ///
 - /// 是否需要弹出提示信息
 - private void fn_getCurrFileList(bool IsAlert)
 - {
 - try
 - {
 - //查找Xmlzip文件夹下 属于其本身UnitCoding的相关zip文件
 - string strXmlZipDirectory = Server.MapPath(\"../Xmlzip/\");
 - if (Directory.Exists(strXmlZipDirectory))
 - {
 - //DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
 - DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory);
 - FileInfo[] FI = di.GetFiles(\"*.zip\");//只查.zip文件
 - if (FI.Length > 0)
 - {
 - lst_DownLoadFileList.Items.Clear();
 - foreach (FileInfo tmpFI in FI)
 - {
 - ListItem tmpItem = new ListItem();
 - tmpItem.Text = tmpFI.Name;
 - lst_DownLoadFileList.Items.Add(tmpItem);
 - }
 - lst_DownLoadFileList.SelectedIndex = 0;
 - }
 - else
 - {
 - if (IsAlert)
 - {
 - Response.write(\"查无可以下载的文件!\");
 - }
 - }
 - }
 - }
 - catch (Exception ex)
 - {
 - ex.ToString();
 - }
 - }
 - #endregion
 
C#文件上传下载及列表相关代码示例就介绍到这里。
                网站名称:C#文件上传下载及列表相关代码示例
                
                文章分享:http://www.csdahua.cn/qtweb/news1/1351.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网