internal protected void OnAfterUploadFile(UploadEventArgs e)
 {
     if (this.AfterUpload != null)
     {
         this.AfterUpload(this, e);
     }
 }
        //public event ImageContentHandler ImageContent;
        #endregion

        internal protected void OnBeforeUploadFile(UploadEventArgs e)
        {
            if (this.BeforeUpload != null)
            {
                this.BeforeUpload(this, e);
            }
        }
		public static void UploadFile(ImageUploader uploadControl, HttpPostedFile file, string originalName, string newName, out string filePath)
		{
            filePath = string.Empty;

			string path = GetUploadRootPath("ImageUploadRootPath");
            string tempPath = Path.Combine(path + @"Temp\", newName);
			AutoCreateUploadPath(path);

			var beforeArgs = new UploadEventArgs(originalName);
			uploadControl.OnBeforeUploadFile(beforeArgs);
			file.SaveAs(Path.Combine(path + @"Temp\", newName));
			var afterArgs = new UploadEventArgs(newName);
			uploadControl.OnAfterUploadFile(afterArgs);

            filePath = tempPath;
		}
        public static void UploadFile(ImageUploader uploadControl, HttpPostedFile file, string originalName, string newName, out string filePath)
        {
            filePath = string.Empty;

            string path     = GetUploadRootPath("ImageUploadRootPath");
            string tempPath = Path.Combine(path + @"Temp\", newName);

            AutoCreateUploadPath(path);

            var beforeArgs = new UploadEventArgs(originalName);

            uploadControl.OnBeforeUploadFile(beforeArgs);
            file.SaveAs(Path.Combine(path + @"Temp\", newName));
            var afterArgs = new UploadEventArgs(newName);

            uploadControl.OnAfterUploadFile(afterArgs);

            filePath = tempPath;
        }
Exemple #5
0
        /// <summary>
        /// 上传
        /// </summary>
        public static void Upload()
        {
            ObjectContextCache.Instance["FileUploadProcessed"] = true;

            HttpRequest  request  = HttpContext.Current.Request;
            HttpResponse response = HttpContext.Current.Response;

            string lockID      = WebUtility.GetRequestQueryValue <string>("lockID", string.Empty);
            string userID      = WebUtility.GetRequestQueryValue <string>("userID", string.Empty);
            string fileName    = WebUtility.GetRequestQueryValue <string>("fileName", string.Empty);
            int    fileMaxSize = WebUtility.GetRequestQueryValue <int>("fileMaxSize", 0);
            string controlID   = WebUtility.GetRequestQueryValue("controlID", string.Empty);

            ExceptionHelper.CheckStringIsNullOrEmpty(fileName, "fileName");

            try
            {
                if (fileMaxSize > 0 && request.ContentLength > fileMaxSize)
                {
                    GenerateErrorInformation(string.Format("文件超过了上传大小的限制{0}字节", fileMaxSize));
                }
                else
                {
                    //不检查锁,沈峥修改
                    //CheckLock(lockID, userID);

                    string uploadPath = GetUploadRootPath();

                    AutoCreateUploadPath(uploadPath);

                    Control control = null;

                    if (string.IsNullOrEmpty(controlID) == false)
                    {
                        control = WebControlUtility.FindControlByID((Page)HttpContext.Current.CurrentHandler, controlID, true);
                    }

                    string newID = UuidHelper.NewUuidString();

                    if (fileName.IndexOf(NewMaterialID) == 0)
                    {
                        fileName = fileName.Replace(NewMaterialID, newID);
                    }
                    else
                    {
                        newID = Path.GetFileNameWithoutExtension(fileName);
                    }

                    if (control != null && control is MaterialControl)
                    {
                        UploadEventArgs args = new UploadEventArgs(fileName);
                        ((MaterialControl)control).OnBeforeUploadFile(args);
                    }

                    if (request.QueryString["upmethod"] == "new")
                    {
                        var dialogControlID = WebUtility.GetRequestQueryValue("dialogControlID", string.Empty);
                        request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName);

                        string output = "<script type='text/javascript'>";
                        output += "window.parent.$find('" + dialogControlID + "').onUploadFinish(1)";
                        output += "</script>";

                        response.Write(output);
                    }
                    else
                    {
                        request.SaveAs(uploadPath + @"Temp\" + fileName, false);
                    }

                    if (control != null && control is MaterialControl)
                    {
                        UploadEventArgs args = new UploadEventArgs(fileName);
                        ((MaterialControl)control).OnAfterUploadFile(args);
                    }

                    string fileIconPath = FileConfigHelper.GetFileIconPath(fileName);

                    response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath));

                    string dateTimeNow = SNTPClient.AdjustedTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    response.AppendHeader("lastUploadTag", "message=" + dateTimeNow);
                    response.AppendHeader("newMaterialID", "message=" + HttpUtility.UrlEncode(newID));
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.ToString();

                if (ex is ExternalException && ((ExternalException)(ex)).ErrorCode == FileTooLargeError &&
                    ex.Source == "System.Web")
                {
                    errorMessage = "您上传的文件过大";
                }

                GenerateErrorInformation(errorMessage);
            }
            finally
            {
                try
                {
                    response.End();
                }
                catch { }
            }
        }
        internal virtual void OnAfterUploadFile(UploadEventArgs e)
        {
            UploadHandler handler = (UploadHandler)Events[AfterUploadEventKey];

            if (handler != null)
                handler(this, e);
        }
		/// <summary>
		/// 上传
		/// </summary>
		public static void Upload()
		{
			ObjectContextCache.Instance["FileUploadProcessed"] = true;

			HttpRequest request = HttpContext.Current.Request;
			HttpResponse response = HttpContext.Current.Response;

			string lockID = WebUtility.GetRequestQueryValue<string>("lockID", string.Empty);
			string userID = WebUtility.GetRequestQueryValue<string>("userID", string.Empty);
			string fileName = WebUtility.GetRequestQueryValue<string>("fileName", string.Empty);
			int fileMaxSize = WebUtility.GetRequestQueryValue<int>("fileMaxSize", 0);
			string controlID = WebUtility.GetRequestQueryValue("controlID", string.Empty);

			ExceptionHelper.CheckStringIsNullOrEmpty(fileName, "fileName");

			try
			{
				if (fileMaxSize > 0 && request.ContentLength > fileMaxSize)
				{
					GenerateErrorInformation(string.Format("文件超过了上传大小的限制{0}字节", fileMaxSize));
				}
				else
				{
					//不检查锁,沈峥修改
					//CheckLock(lockID, userID);

					string uploadPath = GetUploadRootPath();

					AutoCreateUploadPath(uploadPath);

					Control control = null;

					if (string.IsNullOrEmpty(controlID) == false)
						control = WebControlUtility.FindControlByID((Page)HttpContext.Current.CurrentHandler, controlID, true);

					string newID = UuidHelper.NewUuidString();

					if (fileName.IndexOf(NewMaterialID) == 0)
						fileName = fileName.Replace(NewMaterialID, newID);
					else
						newID = Path.GetFileNameWithoutExtension(fileName);

					if (control != null && control is MaterialControl)
					{
						UploadEventArgs args = new UploadEventArgs(fileName);
						((MaterialControl)control).OnBeforeUploadFile(args);
					}

					if (request.QueryString["upmethod"] == "new")
					{
						var dialogControlID = WebUtility.GetRequestQueryValue("dialogControlID", string.Empty);
						request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName);

						string output = "<script type='text/javascript'>";
						output += "window.parent.$find('" + dialogControlID + "').onUploadFinish(1)";
						output += "</script>";

						response.Write(output);
					}
					else
					{
						request.SaveAs(uploadPath + @"Temp\" + fileName, false);
					}

					if (control != null && control is MaterialControl)
					{
						UploadEventArgs args = new UploadEventArgs(fileName);
						((MaterialControl)control).OnAfterUploadFile(args);
					}

					string fileIconPath = FileConfigHelper.GetFileIconPath(fileName);

					response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath));

					string dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
					response.AppendHeader("lastUploadTag", "message=" + dateTimeNow);
					response.AppendHeader("newMaterialID", "message=" + HttpUtility.UrlEncode(newID));
				}
			}
			catch (Exception ex)
			{
				string errorMessage = ex.ToString();

				if (ex is ExternalException && ((ExternalException)(ex)).ErrorCode == FileTooLargeError
					&& ex.Source == "System.Web")
					errorMessage = "您上传的文件过大";

				GenerateErrorInformation(errorMessage);
			}
			finally
			{
				try
				{
					response.End();
				}
				catch { }
			}
		}
		internal protected void OnAfterUploadFile(UploadEventArgs e)
		{
			if (this.AfterUpload != null)
				this.AfterUpload(this, e);
		}
		//public event ImageContentHandler ImageContent;
		#endregion

		internal protected void OnBeforeUploadFile(UploadEventArgs e)
		{
			if (this.BeforeUpload != null)
				this.BeforeUpload(this, e);
		}