asp.net 通過HtmlInputFile控制項上傳檔案的類

來源:互聯網
上載者:User
方法1:判斷副檔名是否合法

 

#regionIsAllowedExtension
///<summary>
///是否允許該副檔名上傳
///</summary>
///<paramname = "hifile">HtmlInputFile控制項</param>
///<returns>允許則返回true,否則返回false</returns>
    public static bool IsAllowedExtension(HtmlInputFile hifile)
    {
            string strOldFilePath = "",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

  方法2:判斷上傳檔案大小是否超過最大值

#region IsAllowedLength
/// <summary>
/// 判斷上傳檔案大小是否超過最大值
/// </summary>
/// <param name="hifile">HtmlInputFile控制項</param>
/// <returns>超過最大值返回false,否則返回true.</returns>
public static bool IsAllowedLength(HtmlInputFile hifile)
{
//允許上傳檔案大小的最大值,可以儲存在xml檔案中,單位為KB
int i = 20;
//如果上傳檔案的大小超過最大值,返回flase,否則返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
{
return false;
}
return true;
}
#endregion

  方法3:獲得唯一的檔案名稱
  為了保證上傳檔案不會出現覆蓋的情況,我們需要根據目前時間對檔案進行重新命名,得到唯一的檔案名稱的方法為:

#region GetUniqueString
/// <summary>
/// 擷取一個不重複的檔案名稱
/// </summary>
/// <returns></returns>
public static string GetUniqueString()
{
//得到的檔案名稱形如:20050922101010
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion

  方法4:刪除檔案

#region DeleteFile
/// <summary>
/// 刪除指定檔案
/// </summary>
/// <param name="strAbsolutePath">檔案絕對路徑</param>
/// <param name="strFileName">檔案名稱</param>
public static 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


  方法5:上傳檔案
  如果上傳檔案的副檔名和大小均合法,則將檔案上傳到伺服器上,方法為:

#region SaveFile
/// <summary>
/// 上傳檔案並返迴文件名
/// </summary>
/// <param name="hifile">HtmlInputFile控制項</param>
/// <param name="strAbsolutePath">絕對路徑.如:Server.MapPath(@"Image/upload")與Server.MapPath(@"Image/upload/")均可,用\符號亦可</param>
/// <returns>返回的檔案名稱即上傳後的檔案名稱</returns>
public static 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

  該方法將檔案上傳後,會返回上傳檔案的新檔案名稱,以備將此新檔案名稱插入到資料庫中。

  方法6:重新上傳檔案
  重新上傳檔案時,首先要將原來上傳過的檔案刪除,然後上傳新檔案,並用新檔案名稱覆蓋資料庫中的舊檔案名稱,這樣才完成了重新上傳工作,實現代碼如下:

#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 static 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.