using System;
using System.Web;
using System.IO;

namespace leo
{
 /// <summary>
 /// 文件上传类
 /// 作者:leo
 /// 制作时间:2006-2-13  最后修改于2007-6-25
 /// </summary>
 public class UploadFile
 {
        public UploadFile(HttpPostedFile PostedFile)
  {
            FileObj = PostedFile;
  }

  private HttpPostedFile FileObj;
  private int     maxSize         = 150 * 1024;             //默认150KB
  private string filesType        = ".jpeg|.jpg|.gif";      //默认文件类型
  private bool   oldFileName      = false;                  //是否采用原文件名
  private string filesPath        = "";                     //设置文件夹路径
  private string Eror             = "";                     //错误信息
  private int errorNum            = 0;                      //错误码

  private string savaFileName     = "";                     //上传成功后的文件名称


  //属性区
  public int MaxSizeKB
  {
   get{ return maxSize / 1024; }
   set{ maxSize = value * 1024;}
  }

  public string FilesType
  {
   get{ return filesType; }
   set{ filesType = value.ToLower();}
  }

  public string FilesPath
  {
   get{ return filesPath; }
   set{ filesPath = value;}
  }

  public string SavaFileName
  {
   get{ return savaFileName; }
   set{ savaFileName = value;}
  }

  public string SaveEror
  {
   get{ return Eror; }
  }

        public int ErrorNum
        {
            get { return errorNum; }
        }

  public bool OldFileName
  {
   get{ return oldFileName; }
   set{ oldFileName = value;}
  }

       
  //方法区
  private string GetNewFileName(string FlieExtension)
  {
   Random rd = new Random();
   int rnum = rd.Next(100, 999);
   string dt = DateTime.Now.ToString("yyyyMMddhhmmss");

   return dt + rnum.ToString() + FlieExtension;
  }

  public bool SaveFile()
  {
   if (FileObj != null)
   {
    try
    {
     string strFileURLName      = FileObj.FileName;                                      //上传文件原URL
     string strFileName         = Path.GetFileName(strFileURLName);              //上传文件原名
     string strFlieExtension    = Path.GetExtension(strFileURLName).ToLower();   //上传文件原扩展名


                    if (FileObj.ContentLength == 0)
                    {
                        Eror      = "您上传的文件为空";
                        errorNum  = 2;
                        return false;
                    }


     if (FileObj.ContentLength > maxSize)
     {
      Eror      = "上传文件超过限定大小";
                        errorNum  = 3;
      return false;
     }

 

                    string[] fType = filesType.Split('|');
                    bool ok = false;
                    for (int i = 0; i < fType.Length; i++)
                    {
                        if (fType[i] == strFlieExtension)
                            ok = true;
                    }
                    if (!ok)
                    {
                        Eror      = "上传文件类型不是程序允许的类型";
                        errorNum  = 4;
                        return false;
                    }

 

     if(!Directory.Exists(filesPath))
     {
      Eror      = "文件存储的目录不存在";
                        errorNum  = 5;
      return false;
     }

 

     string  NewFileName;

     if (!oldFileName)
     {
     NewFileName = GetNewFileName(strFlieExtension);
     }
     else
     NewFileName = strFileName;


     try
     {
      FileObj.SaveAs(Path.Combine(filesPath,NewFileName));
     }
     catch
     {
      Eror      = "存储文件时发生错误,可能是该目录没有权限";
                        errorNum  = 6;
      return false;
     }

     savaFileName = NewFileName;

 

                    Eror     = "";
                    errorNum = 0;
                    return true;
    }
    catch(Exception e)
    {
        Eror     = e.ToString();
                    errorNum = 9;
        return false;
    }
   
   }
   else
   {
    Eror     = "没有文件被上传";
                errorNum = 1;
    return false;
   }
  }

 }
}

posted on 2007-09-06 09:50  绿毛虫  阅读(419)  评论(0编辑  收藏  举报