上传文件类!

来源:互联网 发布:钉钉澡堂模式 知乎 编辑:程序博客网 时间:2024/05/22 02:17

 using System;
using System.Web;
using System.IO;
using System.Web.UI.HtmlControls;
namespace essasoft
{
 /// <summary>
 /// upfile 的摘要说明。
 /// </summary>
 public class upfile
 {

  public upfile()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public static string theLastName;
  public static string theFilePath;
  ///<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;
  }
  

 

    //方法2:判断上传文件大小是否超过最大值

  
  /// <summary>
  /// 判断上传文件大小是否超过最大值
  /// </summary>
  /// <param name="hifile">HtmlInputFile控件</param>
  /// <returns>超过最大值返回false,否则返回true.</returns>
  public static bool IsAllowedLength(HtmlInputFile hifile)
  {
   //允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
   int i = 200;
   //如果上传文件的大小超过最大值,返回flase,否则返回true.
   if(hifile.PostedFile.ContentLength > i * 1024)
   {
    return false;
   }
   return true;
  }
  

  private static Random rnd = new Random();
   //方法3:获得唯一的文件名
   //为了保证上传文件不会出现覆盖的情况,我们需要根据当前时间对文件进行重新命名,得到唯一的文件名的方法为:

  
  /// <summary>
  /// 获取一个不重复的文件名
  /// </summary>
  /// <returns></returns>
  public static string GetUniqueStringName()
  {
   //得到的文件名形如:20050922101010
   const int RANDOM_MAX_VALUE = 1000;
   string strTemp,strYear,strMonth,strDay,strHour,strMinute,strSecond,strMillisecond;

   DateTime dt =DateTime.Now;
   int rndNumber = rnd.Next(RANDOM_MAX_VALUE);
   strYear = dt.Year.ToString ();
   strMonth = (dt.Month > 9)? dt.Month.ToString() : "0" + dt.Month.ToString();
   strDay = (dt.Day > 9)? dt.Day.ToString() : "0" + dt.Day.ToString();
   strHour = (dt.Hour > 9)? dt.Hour.ToString() : "0" + dt.Hour.ToString();
   strMinute = (dt.Minute > 9)? dt.Minute.ToString() : "0" + dt.Minute.ToString();
   strSecond = (dt.Second > 9)? dt.Second.ToString() : "0" + dt.Second.ToString();
   strMillisecond = dt.Millisecond.ToString();

   strTemp = strYear + strMonth + strDay +"_"+ strHour + strMinute + strSecond +"_"+ strMillisecond +"_"+ rndNumber.ToString () ;

   return strTemp;

  }
  

 

  //方法4:删除文件

   
  /// <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);
    }
   }
  }
  


   //方法5:上传文件
   //如果上传文件的扩展名和大小均合法,则将文件上传到服务器上,方法为:

  
  /// <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 = GetUniqueStringName() + strExtension;
    theLastName=strNewFileName;
    theFilePath=strAbsolutePath;
    //如果路径末尾为/符号,则直接上传文件
    if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
    {
     hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
    }
    else
    {
     hifile.PostedFile.SaveAs(strAbsolutePath + "//" + strNewFileName);
    }
   }
   return strNewFileName;
  }
  


   //该方法将文件上传后,会返回上传文件的新文件名,以备将此新文件名插入到数据库中。

   //方法6:重新上传文件
   //重新上传文件时,首先要将原来上传过的文件删除,然后上传新文件,并用新文件名覆盖数据库中的旧文件名,这样才完成了重新上传工作,实现代码如下:

 
  /// <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 = GetUniqueStringName();

   if(ffFile.PostedFile.FileName != string.Empty)
   {
    //旧图片不为空时先删除旧图片
    if(strOldFileName != string.Empty)
    {
     DeleteFile(strAbsolutePath,strOldFileName);
    }
    SaveFile(ffFile,strAbsolutePath);
   }
  }
  


 }
}

原创粉丝点击