日期:2014-05-18 浏览次数:20499 次
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { #region 测试写入 if (!FileUpload1.HasFile) { Response.Write("请选择图片"); } string fileEx = System.IO.Path.GetExtension(this.FileUpload1.FileName); //获取图片扩展名 if (fileEx != ".gif" && fileEx != ".jpg" && fileEx != ".jpeg" && fileEx!= ".bmp") { Response.Write("只能上传图片"); } else { // ImageType 有用,读取图片的时候需要 int imgSize = this.FileUpload1.PostedFile.ContentLength; //图片大小 (字节) string imgType = this.FileUpload1.PostedFile.ContentType; //图片类型 string imgPath = this.FileUpload1.PostedFile.FileName; //获取图片路径 string imgName = this.FileUpload1.FileName; //获取图片名字 int imgLength = this.FileUpload1.FileName.Length; //获取图片长度 if (imgLength <= 0)//判断是否有图片 return; Byte[] imgByte = new Byte[imgSize]; //用图片的长度来初始化一个字节数组存储临时的图片文件 Stream stream = this.FileUpload1.PostedFile.InputStream; //建立文件流对象 stream.Read(imgByte, 0, imgSize);// 读取图片数据到临时存储体imgByte,0为数据指针位置,fileLength为数据长度 //this.FileUpload1.FileBytes try { string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString; SqlConnection sqlconn = new SqlConnection(connection); sqlconn.Open(); string sqlcmd = "insert into Image values (@ImageType,@ImageData,@ImageTitle)"; SqlCommand sqc =
------解决方案--------------------
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { #region 测试读取 string sqlCmd = "select top 1 * from Image order by imageID desc"; string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString; SqlConnection sqlconn = new SqlConnection(connection); try { sqlconn.Open(); SqlCommand sqc = new SqlCommand(sqlCmd, sqlconn); //Response.ContentType = "image/pjpeg"; using (SqlD