⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webbupload2.cs

📁 asp.net 大文件上传,带进度条的时时显示进度效果
💻 CS
字号:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Webb.WAVE.Controls.Upload2
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class WebbUpload2
	{
		#region Fileds
        string m_script;
		private UploadInstance m_uploadInstance	= new UploadInstance();
		#endregion

		#region Properties
		public int M_MaxUploadSize
		{
			set{this.m_uploadInstance.m_MaxSize=value*1024;}
			get{return (int)this.m_uploadInstance.m_MaxSize/1024;}
		}
		public string M_TempPath
		{
			set{this.m_uploadInstance.m_tempPath=value;}
		}
		public string M_UserName
		{
			set{this.m_uploadInstance.m_userName=value;}
		}
		public string UploadGUID
		{
			get{return this.m_uploadInstance.m_UploadGuid;}
		}
		#endregion

		public WebbUpload2()
		{
			//
			// TODO: Add constructor logic here
			//
			#region Script m_script;
			this.m_script	= String.Empty;
			if(UploadHelper.IsAccordantBrowser())
			{
				this.m_script = @"
					<script language=javascript>
					<!--
					url='${url}$';
					var submited = false;
					var m_progressWin;
					function openProgress()
					{
						if(!submited)
						{
							var ary = document.getElementsByTagName('INPUT');
							var openBar = false;
							for(var i=0;i<ary.length;i++)
							{
								var obj = ary[i];
								if(obj.type  == 'file')
								{
									if(obj.value != '')
									{
										openBar = true;
										break;
									}
								}
							}
							if(openBar)
							{
							m_progressWin =	window.showModelessDialog(url, window, 'status:no;help:no;resizable:no;scroll:no;dialogWidth:398px;dialogHeight:200px');
							//	window.showModalDialog(url, window, 'status:no;help:no;resizable:no;scroll:no;dialogWidth:398px;dialogHeight:200px');
							//	event.srcElement.disabled = true;
								submited = true;
							}
							return true;
						}
						else
						{
							event.srcElement.disabled = true;
							return false;
						}
					}
					//-->
					</script>";
			}
			else
			{
				this.m_script = @"
					<script language=javascript>
					<!--
					url='${url}$';
					var submited = false;
					function openProgress()
					{
						if(!submited)
						{
							var ary = document.getElementsByTagName('INPUT');
							var openBar = false;
							for(var i=0;i<ary.length;i++)
							{
								var obj = ary[i];
								if(obj.type  == 'file')
								{
									if(obj.value != '')
									{
										openBar = true;
										break;
									}
								}
							}
							if(openBar)
							{
								var swd = window.screen.availWidth;
								var sht = window.screen.availHeight;
								var wd = 398;
								var ht =170;
								var left = (swd-wd)/2;
								var top = (sht-ht)/2;
								m_progressWin =	window.open(url,'_blank','status=no,toolbar=no,menubar=no,location=no,height='+ht+',width='+wd+',left='+left+',top='+top, true);
							//	event.srcElement.disabled = true;
								submited = true;
							}
							return true;
						}
						else
						{
							event.srcElement.disabled = true;
							return false;
						}
					}
					//-->
					</script>";
			}
			#endregion	
		}

		/// <summary>
		/// Register progress bar to a button.
		/// </summary>
		/// <param name="uploadButton"></param>
		/// <param name="causesValidation"></param>
		public void RegisterProgressBar(Button uploadButton, bool causesValidation)
		{
			if (causesValidation)
			{
				uploadButton.CausesValidation = false;
				uploadButton.Attributes["onclick"] = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();if(!Page_BlockSubmit){openProgress();}";
			}
			else
			{
				uploadButton.Attributes["onclick"] = "openProgress();";
			}
			string m_processURL	= "UploadStatusBar.ashx?UploadGUID="+this.m_uploadInstance.m_UploadGuid;
			this.m_script	= this.m_script.Replace("${url}$", m_processURL);
			Page page		= (Page)(HttpContext.Current.Handler);
		//	page.RegisterHiddenField("WebbUploadGuid", m_uploadStatus.UploadGuid);			
			page.RegisterStartupScript("ProgressScript", this.m_script);
			this.m_uploadInstance.UpdateApplication();
		//	page.RegisterHiddenField("Upload_GUID", m_uploadStatus.UploadGuid);
			if(HttpContext.Current.Response.Cookies["Upload_GUID"]!=null)
			{
				HttpContext.Current.Response.Cookies.Set(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
			else
			{
				HttpContext.Current.Response.Cookies.Add(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
		}

		public void RegisterProgressBar(WebControl m_controls)
		{
			//m_controls.Attributes["onclick"] = "if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }} this.disabled = true;openProgress();__doPostBack('btn_upload','');if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
			m_controls.Attributes["onclick"] = "openProgress();";
			this.m_uploadInstance.m_status	= UploadInstance.UploadStatus.Initialization;			
			string progressUrl	= "UploadStatusBar.ashx?UploadGUID="+this.m_uploadInstance.m_UploadGuid;
			this.m_script	= this.m_script.Replace("${url}$", progressUrl);
			Page page	= (Page)(HttpContext.Current.Handler);	
			//page.RegisterHiddenField("Webb_Upload_GUID", this.m_uploadInstance.m_UploadGuid);
			page.RegisterStartupScript("ProgressScript", this.m_script);
			if(HttpContext.Current.Response.Cookies["Upload_GUID"]!=null)
			{
				HttpContext.Current.Response.Cookies.Set(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
			else
			{
				HttpContext.Current.Response.Cookies.Add(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
			this.m_uploadInstance.UpdateApplication();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="uploadButton"></param>
		/// <param name="causesValidation"></param>
		public void RegisterProgressBar(LinkButton uploadButton, bool causesValidation)
		{
			if (causesValidation)
			{
				uploadButton.CausesValidation = false;
				uploadButton.Attributes["onclick"] = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();if(!Page_BlockSubmit){openProgress();}";
			}
			else
			{
				uploadButton.Attributes["onclick"] = "openProgress();";
			}
			string m_processURL	= "UploadStatusBar.ashx?UploadGUID="+this.m_uploadInstance.m_UploadGuid;
			this.m_script	= this.m_script.Replace("${url}$", m_processURL);
			Page page		= (Page)(HttpContext.Current.Handler);
		//	page.RegisterHiddenField("WebbUploadGuid", m_uploadStatus.UploadGuid);			
			page.RegisterStartupScript("ProgressScript", this.m_script);
			m_uploadInstance.UpdateApplication();
		//	page.RegisterHiddenField("Upload_GUID", m_uploadStatus.UploadGuid);
			if(HttpContext.Current.Response.Cookies["Upload_GUID"]!=null)
			{
				HttpContext.Current.Response.Cookies.Set(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
			else
			{
				HttpContext.Current.Response.Cookies.Add(new HttpCookie("Upload_GUID",m_uploadInstance.m_UploadGuid));
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="i_fileName"></param>
		/// <returns></returns>
		public UploadFileCollection GetUploadedFileCollection(string i_fieldName)
		{
			if(HttpContext.Current.Request[i_fieldName]==null)return null;
			UploadFileCollection m_uploadFiles	= new UploadFileCollection();
            string[] m_uploadFilesContent	= HttpContext.Current.Request[i_fieldName].ToString().Split(',');
			foreach(string m_fileContent in m_uploadFilesContent)
			{
				//Content-Type: application/octet-stream; filename=""; filename_server=""
				if(m_fileContent==string.Empty)continue;
				string[] m_fileData		= m_fileContent.Split(';');
				if(m_fileData[1].IndexOf("\"\"")>0||m_fileData[2].IndexOf("\"\"")>0) continue;
				UploadFile	m_file		= new UploadFile();
				m_file.FullPathOnClient	= m_fileData[1].Substring(11,m_fileData[1].Length-12);
				string m_tempPath		= m_fileData[2].Substring(18,m_fileData[2].Length-19);
				m_file.FullPathOnServer	= m_tempPath;
				m_uploadFiles.Add(m_file);
			}
			return m_uploadFiles;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="i_fieldName"></param>
		/// <returns></returns>
		public UploadFile GetUploadFile(string i_fieldName)
		{
			if(HttpContext.Current.Request[i_fieldName]==null)return null;
			UploadFileCollection m_uploadFiles	= new UploadFileCollection();
			string[] m_uploadFilesContent	= HttpContext.Current.Request[i_fieldName].ToString().Split(',');
			if(m_uploadFilesContent.Length!=1)return null;
			string[] m_fileData		= m_uploadFilesContent[0].Split(';');
			if(m_fileData[1].IndexOf("\"\"")>0||m_fileData[2].IndexOf("\"\"")>0) return null;
			UploadFile	m_file		= new UploadFile();
			m_file.FullPathOnClient	= m_fileData[1].Substring(11,m_fileData[1].Length-12);
			m_file.FullPathOnServer	= m_fileData[2].Substring(18,m_fileData[2].Length-19);
			return  m_file;
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public DateTime GetUploadStartTime()
		{
            if(HttpContext.Current.Request["WebbUploadStartTime"]==null)return DateTime.MinValue;
			DateTime m_startTime;
			try
			{
				m_startTime	= Convert.ToDateTime(HttpContext.Current.Request["WebbUploadStartTime"]);
				return m_startTime;
			}
			catch
			{
				return DateTime.MinValue;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="i_Path"></param>
		public void SetUploadPath(string i_Path)
		{
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("Upload_Path",i_Path));
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -