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;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace CommonFunction
{
	/// <summary>
	/// UpImage 的摘要说明。
	/// </summary>
	public class UpImage : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button BtnUpFile;
		protected System.Web.UI.HtmlControls.HtmlInputFile UpFileControl;
		protected System.Web.UI.WebControls.Button btnDown;
		protected string imagePath,imageType,imageName;

		protected int imageSize;
		protected byte[] imageBody;
		protected string SqlCom = "";
		SqlConnection con = new SqlConnection();
		SqlCommand cmd = new SqlCommand();

		private void Page_Load(object sender, System.EventArgs e)
		{
			con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
		}

		#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.BtnUpFile.Click += new System.EventHandler(this.BtnUpFile_Click);
			this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void BtnUpFile_Click(object sender, System.EventArgs e)
		{
			if("" != UpFileControl.PostedFile.FileName)
			{
				imagePath= UpFileControl.PostedFile.FileName;
				imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
				imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);

				if("jpg" != imageType && "gif" != imageType)
				{
					Response.Write("<script language='javascript'> alert('对不起！请您选择jpg或者gif格式的图片！');</script>");
					return;
				}
				else
				{
					HttpPostedFile SendImage=UpFileControl.PostedFile;
					imageSize=SendImage.ContentLength;////取得图片
					imageBody=new Byte[imageSize];
					
					Stream StreamObject=SendImage.InputStream;//建立数据流对象
					StreamObject.Read(imageBody,0,imageSize);///把图像数据放到mMarkBody中,其中0代表数据指针位置,intFileLength代表要读取的流的长度

					imagePath=UpFileControl.PostedFile.FileName;//取得图象路径

					SqlCom="insert into Image(ImageName,Image,ImageSize) values('"+imageName+"','"+imageBody+"','"+imageSize.ToString()+"')";
					con.Open();
					cmd = new SqlCommand(SqlCom,con);
					try
					{
						cmd.ExecuteNonQuery();
						Response.Write("成功!");
					}
					catch
					{
						Response.Write("失败!");
					}
					con.Close();
				}
			}
		}

		private void btnDown_Click(object sender, System.EventArgs e)
		{
		
			SqlCom="select top 1 image,imageName from image order by ID desc";

			cmd=new SqlCommand(SqlCom,con);

			con.Open();

			SqlDataReader myReader=cmd.ExecuteReader();

			try
			{
				myReader.Read();
			}
			catch
			{
				Response.Write("出现异常！");
			}

			Response.Buffer=true;  
			Response.Clear();  
			Response.ContentType="gif";  
			Response.AddHeader("Content-Disposition","attachment;filename="+myReader["imageName"].ToString() + ";"); 
	
			byte[] file = (byte[])myReader["Image"];
			Response.BinaryWrite(file);  
			Response.Flush();  
			Response.End();  
			con.Close();
			myReader.Close();
		}

		private void btnSee_Click(object sender, System.EventArgs e)
		{
		
		}
	}
}
