コード例 #1
0
ファイル: FileHandler.cs プロジェクト: cosmoto91/Projects
 public void SaveFile(string destination, System.Web.UI.WebControls.FileUpload file)
 {
     if (file.HasFile)
     {
         file.SaveAs(destination + file.FileName);
         file.Dispose();
     }
 }
コード例 #2
0
 public static SaveFileResult SaveFile(string strAcct, FileUpload FileUpload, string SaveFilePath, int MaxKBSize, params string[] Extensions)
 {
     SaveFileResult result = null;
     result = new SaveFileResult();
     if ((FileUpload != null) && FileUpload.HasFile)
     {
         if (string.IsNullOrEmpty((SaveFilePath ?? "").Trim()))
         {
             result.Msg = "未設定儲存路徑";
             return result;
         }
         if ((MaxKBSize > 0) && (FileUpload.PostedFile.ContentLength > (MaxKBSize * 0x400)))
         {
             result.Msg = "超出大小限制";
             return result;
         }
         if (Extensions.Length > 0)
         {
             bool flag = false;
             foreach (string str in Extensions)
             {
                 if (Path.GetExtension(FileUpload.FileName).ToLower() == ("." + str.ToLower()))
                 {
                     flag = true;
                     break;
                 }
             }
             if (!flag)
             {
                 result.Msg = "不是允許的副檔名";
                 return result;
             }
         }
         if (Strings.Right(SaveFilePath, 1) != "/")
         {
             SaveFilePath = SaveFilePath + "/";
         }
         SetFolder(SaveFilePath);
         string str2 = strAcct + DateTime.Now.ToString("yyMMddHHmmssfff") + Path.GetExtension(FileUpload.FileName);
         string filename = HttpContext.Current.Server.MapPath(SaveFilePath + str2);
         FileUpload.SaveAs(filename);
         FileUpload.Dispose();
         result.Result = true;
         result.Msg = SaveFilePath + str2;
     }
     return result;
 }
コード例 #3
0
ファイル: FileUpLoadCommon.cs プロジェクト: mind0n/hive
 /// <summary>
 /// 保存文件
 /// </summary>
 /// <param name="fu">上传文件对象</param>
 /// <param name="IsImage">是否图片</param>
 /// <returns>成功/失败</returns>
 public bool SaveFile(FileUpload fu, bool IsImage)
 {
     bool rBool = false;
     rBool = SaveFile(fu.PostedFile, IsImage);
     fu.Dispose();
     return rBool;
 }
コード例 #4
0
        /// &ltsummary>
        /// 保存文件
        /// </summary>
        /// &ltparam name="fu">上传文件对象</param>
        /// &ltparam name="IsImage">是否邮件</param>
        public bool SaveFile(FileUpload fu)
        {
            if (fu.HasFile)
            {
                string fileContentType = fu.PostedFile.ContentType;
                string name = fu.PostedFile.FileName;                        // 客户端文件路径
                FileInfo file = new FileInfo(name);
                _fileType = fu.PostedFile.ContentType;
                _fileSize = fu.PostedFile.ContentLength;
                bool isfileTypeImages = false;
                if (fileContentType == "image/x-png" || fileContentType == "image/png" || fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                {
                    isfileTypeImages = true;
                }

                //检测文件扩展名是否正确
                var ImgExtention = file.Extension.Substring(1).ToLower();

                if (ImgExtention != "gif" && ImgExtention != "jpg" && ImgExtention != "jpeg" && ImgExtention != "png")
                {
                    _errorMsg = string.Format("文件扩展名不符合系统需求:{0}",
                        "gif|jpg|jpeg|png");
                    fu.Dispose();
                    return false;
                }
                if (_fileSize / 1024 > 2048)
                {
                    _errorMsg = string.Format("上传文件超过系统允许大小:{0}K", "2048");
                    fu.Dispose();
                    return false;
                }
                _fileName = file.Name;                                // 文件名称
                _newFileName = CreateFileName() + file.Extension;
                _webFilePath = HttpContext.Current.Server.MapPath(_path + _newFileName);         // 服务器端文件路径
                if (isfileTypeImages)
                {
                      //检查文件是否存在
                    if (!File.Exists(_webFilePath))
                    {
                        try
                        {
                            fu.SaveAs(_webFilePath);                                 // 使用 SaveAs 方法保存文件

                            // 只有上传完了,才能获取图片大小
                            if (File.Exists(_webFilePath))
                            {
                                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(_webFilePath);
                                try
                                {
                                    _fileHeight = originalImage.Height;
                                    _fileWidth = originalImage.Width;
                                }
                                finally
                                {
                                    originalImage.Dispose();
                                }
                            }
                            _errorMsg = string.Format("提示:文件“{0}”成功上传,文件类型为:{1},文件大小为:{2}B", _newFileName, fu.PostedFile.ContentType, fu.PostedFile.ContentLength);
                            fu.Dispose();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            _errorMsg = "提示:文件上传失败,失败原因:" + ex.Message;
                        }
                    }
                    else
                    {
                        _errorMsg = "提示:文件已经存在,请重命名后上传";
                    }
                }
                else
                {
                    //上传文件
                    //检查文件是否存在
                    if (!File.Exists(_webFilePath))
                    {
                        try
                        {
                            fu.SaveAs(_webFilePath);                                 // 使用 SaveAs 方法保存文件
                            _errorMsg = string.Format("提示:文件“{0}”成功上传,文件类型为:{1},文件大小为:{2}B", _newFileName,  fu.PostedFile.ContentType, fu.PostedFile.ContentLength);
                            fu.Dispose();
                            return true;
                        }
                        catch (Exception ex)
                        {
                            _errorMsg = "提示:文件上传失败,失败原因:" + ex.Message;
                        }
                    }
                    else
                        _errorMsg = "提示:文件已经存在,请重命名后上传";
                }
            }
            fu.Dispose();
            return false;
        }
コード例 #5
0
        protected string saveFile(string fileExtension, System.Web.UI.WebControls.FileUpload fileUploads)
        {
            string alertMessage = string.Empty;

            string[] docList         = null;
            string   emplId          = string.Empty;
            string   fileName        = string.Empty;
            string   fileType        = string.Empty;
            string   msg             = string.Empty;
            string   templateId      = string.Empty;
            string   searchCriteria  = string.Empty;
            string   uploadDirectory = string.Empty;
            string   uploadFilePath  = string.Empty;

            try
            {
                emplId = Session["EmplId"].ToString();

                if (fileUploads.HasFile)
                {
                    fileName = Path.GetFileName(fileUploads.FileName);
                    fileType = Path.GetExtension(fileName).ToLower();

                    // Check for Supported file types
                    if (fileType == string.Concat(".", fileExtension))
                    {
                        //Build the full file path for the upload directory with file name
                        uploadDirectory = string.Concat(Server.MapPath(Session["UploadDirectory"].ToString()), emplId);
                        uploadFilePath  = string.Concat(uploadDirectory, "\\", fileName);

                        if (!Directory.Exists(uploadDirectory))
                        {
                            Directory.CreateDirectory(uploadDirectory);
                        }
                        else // Only one file type extension should exist in the directory
                        {
                            searchCriteria = string.Concat("*.", fileExtension);

                            docList = Directory.GetFiles(uploadDirectory, searchCriteria);

                            foreach (string uploadedFile in docList)
                            {
                                File.Delete(uploadedFile);
                            }
                        }

                        fileUploads.PostedFile.SaveAs(uploadFilePath);
                        fileUploads.Dispose();

                        FileInfo fi = new FileInfo(uploadFilePath);

                        //File attribute can't be read only, change to normal
                        if (fi.IsReadOnly)
                        {
                            File.SetAttributes(uploadFilePath, FileAttributes.Normal);
                        }

                        fi          = null;
                        fileUploads = null;

                        // Log the Action
                        msg = string.Format("Create Template Screen - Save File: {0} to {1}",
                                            fileName, uploadFilePath);
                        _HRSCLogsDA.Insert(msg);
                    }
                    else
                    {
                        //Display message
                        msg          = string.Concat("Only a ", fileExtension, " file may be Uploaded");
                        alertMessage = "alert('" + msg + "');";
                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Information Message", alertMessage, true);
                    }
                }
                else
                {
                    //Display message
                    msg          = string.Concat("A File Upload is required to be selected. Please use the Browse button for selecting the file to be uploaded.");
                    alertMessage = "alert('" + msg + "');";
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Information Message", alertMessage, true);
                }
            }
            catch (InternalException err)
            {
                // Display a Messagebox
                AlertMessage.Show(err.UserFriendlyMsg, this.Page);
            }
            catch (Exception err)
            {
                // Error
                string errMsg = string.Format("{0} - Upload {1} Click Error - {2}",
                                              GetType().FullName,
                                              fileExtension,
                                              err.Message);

                // Log the Error
                AppLogWrapper.LogError(errMsg);

                // Save a User Friendly Error Message in Session Variable
                errMsg = string.Concat("There was a problem uploading the ", fileExtension, " file.  If the problem persists, please contact Technical Support.");

                // Display a Messagebox
                AlertMessage.Show(errMsg, this.Page);
            }
            return(uploadFilePath);
        }