日期:2014-05-20 浏览次数:20997 次
ReadImage.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Data.SqlClient; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridShowImage { /// <summary> /// ReadImage 的摘要说明。 /// </summary> public class ReadImage : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string strImageID = Request.QueryString["id"]; SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"); SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" + strImageID, myConnection); try { myConnection.Open(); SqlDataReader myDataReader; myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); if(myDataReader.Read()) { Response.Clear(); Response.ContentType = myDataReader["PersonImageType"].ToString(); Response.BinaryWrite((byte[])myDataReader["PersonImage"]); } myConnection.Close(); } catch (SqlException SQLexc) { } Response.End(); } #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.Load += new System.EventHandler(this.Page_Load); } #endregion } }
------解决方案--------------------
lz参考下 图片转成2进制 放入数据库 asp.net 上
protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile.FileName != "") { string name = FileUpload1.PostedFile.FileName; string type = name.Substring(name.LastIndexOf(".") + 1); FileStream fs = File.OpenRead(name); byte[] content = new byte[fs.Length]; fs.Read(content, 0, content.Length); fs.Close(); SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand cmd = con.CreateCommand(); con.Open(); cmd.CommandText = "insert into test(image_data) values (@image_data)"; cmd.CommandType = CommandType.Text; if (type == "jpg" || type == "gif" || type == "bmp" || type == "png") { SqlParameter para = cmd.Parameters.Add("@image_data", SqlDbType.Image); para.Value = content; cmd.ExecuteNonQuery(); Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!')</script>"); } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择图片类型的文件!')</script>"); } }