C#文件上傳下載及列表相關(guān)代碼示例
作者:佚名
這里將介紹C#文件上傳下載及列表相關(guān)代碼示例,這些示例將幫助大家更好的理解C#的功能機制,希望對大家有所幫助。
C#文件上傳下載是對文件的基本操作,C#文件上傳下載主要實現(xiàn)的是對文件的管理與應(yīng)用。這里介紹的代碼將實現(xiàn)大部分功能。
1.文件上傳
如下要點:
HTML部分:
- <form id=\"form1\" runat=\"server\" method=\"post\" enctype=\"multipart/form-data\">
- <input id=\"FileUpLoad\" type=\"file\" runat=\"server\"/><br />
- 后臺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值 來實現(xiàn)點擊后實現(xiàn)文件下載的超級連接
this.Label1.Text = \"<a href=\\\"Xmlzip/a.rar\\\">a.rar</a>\"
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)前可用的文件列表
- /// <summary>
- /// 得到當(dāng)前可用的文件列表
- /// </summary>
- /// <param name=\"IsAlert\">是否需要彈出提示信息</param>
- 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)代碼示例就介紹到這里。
【編輯推薦】
責(zé)任編輯:彭凡
來源:
中國IT實驗室