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

為寒亭等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及寒亭網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、寒亭網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1.文件上傳
如下要點(diǎn):
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事件 設(shè)定相關(guān)下載連接
- 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值 來實(shí)現(xiàn)點(diǎn)擊后實(shí)現(xiàn)文件下載的超級連接
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 得到當(dāng)前可用的文件列表
- ///
- /// 得到當(dāng)前可用的文件列表
- ///
- /// 是否需要彈出提示信息
- private void fn_getCurrFileList(bool IsAlert)
- {
- try
- {
- //查找Xmlzip文件夾下 屬于其本身UnitCoding的相關(guān)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#文件上傳下載及列表相關(guān)代碼示例就介紹到這里。
本文標(biāo)題:C#文件上傳下載及列表相關(guān)代碼示例
URL分享:http://www.dlmjj.cn/article/copeiii.html


咨詢
建站咨詢
