.net 操作檔案上傳

iDotNetSpace發表於2010-10-12
  ///
        /// 上傳檔案
        ///

        /// 檔案
        /// 檔案上傳路徑
        /// bool
        private static bool SaveFile(FileUpload fileUpload,string path)
        {
            //上傳
            fileUpload.SaveAs(HttpContext.Current.Server.MapPath(path));
            return true;
        }

        ///


        /// 上傳檔案 檔案型別 大小KB 路徑 必須在指定範圍以內
        ///

        /// 要上傳檔案
        /// 上傳路徑
        /// 陣列:檔案字尾名
        /// 檔案大小KB
        /// bool
        private static bool SaveFile(FileUpload fileUpload, string Path, string[] FileType, int length,ref string msg)
        {
            //獲取檔案要儲存的路徑   //從配置檔案讀取
            string FilePath = Config.ConfigHelper.GetConfigString("FilePath");
            //獲取檔案字尾名
            string strEx = Path.Substring(Path.LastIndexOf("."));
            //定義是否包含陣列內的檔案格式
            bool ft = false;
            //得到FileType陣列長度
            int len = FileType.Length;
            string strType="";
            for (int i = 0; i < len; i++)
            {
                if (FileType[i].ToLower() == strEx.ToLower())
                {
                    ft = true;
                    break;
                }
                strType += FileType[i] + "或";
            
            }
          
           //如果包含檔案字尾名
                if (ft)
                {

                    if (((int.Parse(fileUpload.FileContent.Length.ToString())) / 1024) < length)
                    {
                        if (Path != "")
                        {
                            string strPath = FilePath + Path;
                            SaveFile(fileUpload,strPath);
                            return true;
                        }
                        else
                        {
                            //目錄為空
                            return false;
                        }
                    }
                    else
                    {
                        //大小超出範圍
                        string s = "對不起,您上傳檔案的大小超出範圍!不能超過" + length + "KB";
                                             msg = s;
                        return false;
                    }
                }
                else
                {
                    //字尾名不合格
                    strType = strType.Substring(0, strType.Length - 1);
                    string s = "對不起,您上傳檔案的格式不正確!標準格式應為:" + strType;
                                   msg = s;
                    return false;
                }
        }
        ///


        /// 上傳檔案
        ///

        /// 上傳檔案
        /// 路徑 資料夾+檔名
        /// 檔案型別
        /// string path路徑
        public static string SaveFile(FileUpload fileUpload, string path, string[] FileType,ref string msg)
        {
            //if (fileUpload.HasFile)
            //{
                //獲取物理路徑
                string FileName = fileUpload.PostedFile.FileName;
                //獲取字尾名
                string strEx = FileName.Substring(FileName.LastIndexOf("."));

 

                //獲取檔案大小KB  //從配置檔案讀取
                int FileSize = Config.ConfigHelper.GetConfigInt("FileSize");
                //獲取檔案要儲存路徑  //從配置檔案讀取
                string FilePath = Config.ConfigHelper.GetConfigString("FilePath");
                //
                if (path != "")
                {
                    string str=path.Substring(0,path.LastIndexOf("/"));
                    string strPath = HttpContext.Current.Server.MapPath(FilePath + str);
                    //如果原始檔夾不存在則建立
                    if (!Directory.Exists(strPath))
                    {

                        Directory.CreateDirectory(strPath);

                    }
                    SaveFile(fileUpload, path, FileType, FileSize,ref msg);
                    return path;
                }
                //新增新檔案   此處用作非圖片檔案上傳執行
                else
                {
                    //獲取當前年月份
                    string str = DateTime.Now.ToString("yyyyMM");
                    string strGui;
                    string strPath = HttpContext.Current.Server.MapPath(FilePath + str);
                    //如果原始檔夾不存在則建立
                    if (!Directory.Exists(strPath))
                    {

                        //DirectoryInfo d = new DirectoryInfo(str);
                        Directory.CreateDirectory(strPath);

                    }
                    //重新賦檔名
                    string GuidName = Guid.NewGuid() + strEx;
                    strGui = str + "/" + GuidName;
                    SaveFile(fileUpload, strGui, FileType, FileSize,ref msg);
                    return strGui;
                }
            //}
            //else
            //{

            //    msg = "對不起,請點選瀏覽選擇檔案!";
            //    return "";
            //}
        }
        ///


        /// 上傳圖片
        ///

        /// 圖片檔案
        /// 圖片路徑  路徑不為空表示更新  為空表示新新增
        /// string 路徑
        public static string SaveImage(FileUpload FileUpload, string path,ref string msg)
        {
            if (FileUpload.HasFile)
            {
                //獲取物理路徑
                string FileName = FileUpload.PostedFile.FileName;
                //獲取字尾名
                string strEx = FileName.Substring(FileName.LastIndexOf("."));
                bool ft= false;
                string strType = "";
                //檔案的格式
                string[] FileType = { ".jpg", ".gif" };

                //迴圈驗證是否是合法格式
                for (int i = 0; i < FileType.Length; i++)
                {
                    if (FileType[i].ToLower() == strEx.ToLower())
                    {
                        ft = true;
                        break;
                    }
                    strType += FileType[i] + "或";

                }
                //格式不正確  返回資訊
                if (!ft)
                {
                    strType = strType.Substring(0, strType.Length - 1);
                    msg = "對不起,您上傳檔案的格式不正確!標準格式應為:" + strType;
                }
                //獲取檔案大小KB   //從配置檔案讀取
                int FileSize = Config.ConfigHelper.GetConfigInt("ImgSize");
                //獲取檔案要儲存路徑
                string FilePath = Config.ConfigHelper.GetConfigString("FilePath");
                //路徑不為空表示更新   檔名不變  覆蓋原檔案
                if (path != "")
                {
                    SaveFile(FileUpload, path, FileType, FileSize, ref msg);
                    return path;
                }
                //新增新檔案
                else
                {
                    //獲取當前年月份
                    string str = DateTime.Now.ToString("yyyyMM");
                    string strGui;
                    string strPath = HttpContext.Current.Server.MapPath(FilePath + str);
                    //如果原始檔夾不存在則建立
                    if (!Directory.Exists(strPath))
                    {

                        //DirectoryInfo d = new DirectoryInfo(str);
                        Directory.CreateDirectory(strPath);

                    }
                    //重新賦檔名
                    string GuidName = Guid.NewGuid() + strEx;
                    strGui = str + "/" + GuidName;
                    SaveFile(FileUpload, strGui, FileType, FileSize, ref msg);
                    return strGui;
                }
            }
            else
            {
                msg = "對不起,請點選瀏覽選擇檔案!";
                return "";
            }
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-675758/,如需轉載,請註明出處,否則將追究法律責任。

相關文章