using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CommonFunction
{
	/// <summary>
	/// upMoreFile 的摘要说明。
	/// </summary>
	public class upMoreFile : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button Upload;
		protected System.Web.UI.WebControls.Label Title;
		protected System.Web.UI.WebControls.Label strStatus;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			Title.Text = "<h3>多文件上传</h3>";
			Upload.Text = "开始上传";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Upload.Click += new System.EventHandler(this.Upload_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Upload_Click(object sender, System.EventArgs e)
		{	
			upMorefile();
		}

		private bool upMorefile()
		{
			//遍历File表单元素
			System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
			//状态信息
			System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为：<hr color=red>");
			int fileCount;
			int filecount = files.Count;
			try
			{
				for(fileCount = 0;fileCount<files.Count;fileCount++)
				{
					//定义访问客户端上传文件的对象
					System.Web.HttpPostedFile postedFile = files[fileCount];
					string fileName, fileExtension;
					//取得上传得文件名
					fileName = System.IO.Path.GetFileName(postedFile.FileName);
					if(fileName != String.Empty)
					{
						//取得文件的扩展名
						fileExtension = System.IO.Path.GetExtension(fileName);
						//上传的文件信息
						strMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
						strMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
						strMsg.Append("上传文件的文件名：" + fileName + "<br>");
						strMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr color=red>");
						//保存到指定的文件夹
						postedFile.SaveAs(Server.MapPath("upedFile/") + fileName);
					}
				}
				strStatus.Text = strMsg.ToString();
				return true;
			}
			catch(System.Exception error)
			{
				strStatus.Text = error.Message;
				return false;

			}	
		}
	}
}
