using System.IO;
//檢查上傳文件不爲空
if(File1.PostedFile!=null)
{
string nam = File1.PostedFile.FileName ;
//取得文件名(抱括路徑)裏最後一個"."的索引
int i= nam.LastIndexOf(".");
//取得文件擴展名
string newext =nam.Substring(i);
//這裏我自動根據日期和文件大小不同爲文件命名,確保文件名不重複
DateTime now = DateTime.Now;
string newname=now.DayOfYear.ToString()+File1.PostedFile.ContentLength.ToString();
//保存文件到你所要的目錄,這裏是IIS根目錄下的upload目錄.你可以改變.
//注意: 我這裏用Server.MapPath()取當前文件的絕對目錄.在asp.net裏""必須用""代替
File1.PostedFile.SaveAs(Server.MapPath("upload"+newname+newext));
this.HyperLink1.NavigateUrl ="upload"+newname+newext;
//得到這個文件的相關屬性:文件名,文件類型,文件大小
//fname.Text=File1.PostedFile.FileName;
//fenc.Text=File1.PostedFile.ContentType ;
//fsize.Text=File1.PostedFile.ContentLength.ToString();
}
上傳可以用.net裏的HTML控件裏的File Field的上傳控件呀,你拖到窗體上後,你可以右擊做爲服務器端控件使用,就這樣寫上你要上傳的幾句代碼就行了,下載直接連接到你要下載的文件就可以下載了
把文件上傳放到服務器上,直接加超銜接就是下載了.
一下是上傳文件用到的類:
說明:直接在cs文件裏複製粘貼就可以用的.
using System;
using System.IO;
using System.Web.UI.HtmlControls;
namespace youjian
{
/// <summary>
/// UpFile 的摘要說明。
/// </summary>
public class UpFile
{
public UpFile()
{
}
#region 是否允許該擴展名上傳IsAllowedExtension
///<summary>
///是否允許該擴展名上傳
///</summary>
///<paramname = "hifile">HtmlInputFile控件</param>
///<returns>允許則返回true,否則返回false</returns>
public bool IsAllowedExtension(HtmlInputFile hifile)
{
string strOldFilePath = "";
string strExtension = "";
//允許上傳的擴展名,可以改成從配置文件中讀出
string[]arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上傳文件的擴展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判斷該擴展名是否合法
for(int i = 0;i<arrExtension.Length;i++)
{
if(strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
#endregion
#region 判斷上傳文件大小是否超過最大值IsAllowedLength
/// <summary>
/// 判斷上傳文件大小是否超過最大值
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <returns>超過最大值返回false,否則返回true.</returns>
public bool IsAllowedLength(HtmlInputFile hifile)
{
//允許上傳文件大小的最大值,可以保存在xml文件中,單位爲KB
int i = 20;
//如果上傳文件的大小超過最大值,返回flase,否則返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
{
return false;
}
return true;
}
#endregion
#region 獲取一個不重複的文件名GetUniqueString
/// <summary>
/// 獲取一個不重複的文件名
/// </summary>
/// <returns></returns>
public string GetUniqueString()
{
//得到的文件名形如:20050922101010
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion
#region 刪除指定文件DeleteFile
/// <summary>
/// 刪除指定文件
/// </summary>
/// <param name="strAbsolutePath">文件絕對路徑</param>
/// <param name="strFileName">文件名</param>
public void DeleteFile(string strAbsolutePath, string strFileName)
{
//判斷路徑最後有沒有/符號,沒有則自動加上
if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
{
//判斷要刪除的文件是否存在
if(File.Exists(strAbsolutePath + strFileName))
{
//刪除文件
File.Delete(strAbsolutePath + strFileName);
}
}
else
{
if(File.Exists(strAbsolutePath + "//" + strFileName))
{
File.Delete(strAbsolutePath + "//" + strFileName);
}
}
}
#endregion
#region 上傳文件並返回文件名 SaveFile
/// <summary>
/// 上傳文件並返回文件名
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <param name="strAbsolutePath">絕對路徑.如:Server.MapPath(@"Image/upload")與Server.MapPath(@"Image/upload/")均可,用/符號亦可</param>
/// <returns>返回的文件名即上傳後的文件名</returns>
public string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
{
string strOldFilePath = "",strExtension = "",strNewFileName = "";
//如果上傳文件的文件名不爲空
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上傳文件的擴展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//文件上傳後的命名
strNewFileName = GetUniqueString() + strExtension;
//如果路徑末尾爲/符號,則直接上傳文件
if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
{
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
}
else
{
hifile.PostedFile.SaveAs(strAbsolutePath + "//" + strNewFileName);
}
}
return strNewFileName;
}
#endregion
#region 重新上傳文件,刪除原有文件CoverFile
/// <summary>
/// 重新上傳文件,刪除原有文件
/// </summary>
/// <param name="ffFile">HtmlInputFile控件</param>
/// <param name="strAbsolutePath">絕對路徑.如:Server.MapPath(@"Image/upload")與Server.MapPath(@"Image/upload/")均可,用/符號亦可</param>
/// <param name="strOldFileName">舊文件名</param>
public void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
{
//獲得新文件名
string strNewFileName = GetUniqueString();
if(ffFile.PostedFile.FileName != string.Empty)
{
//舊圖片不爲空時先刪除舊圖片
if(strOldFileName != string.Empty)
{
DeleteFile(strAbsolutePath,strOldFileName);
}
SaveFile(ffFile,strAbsolutePath);
}
}
#endregion
C#.net 文件操作:上傳 下載 刪除 文件列表
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事件 設定相關下載連接
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 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 得到當前可用的文件列表
/// <summary>
/// 得到當前可用的文件列表
/// </summary>
/// <param name="IsAlert">是否需要彈出提示信息</param>
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();
}
}