asp.NetUpdatePanel更新なしアップロード画像を実現

4885 ワード

1)フロント
 
  




<br/> <br/> <br/><form id="form1" runat="server"> <br/><div> <br/><updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional"> <br/><contenttemplate> <br/><scriptmanager id="ScriptManager1" runat="server"> <br/></scriptmanager> <br/><button id="Button1" runat="server" onclick="Button1_Click" text="Button"/> <br/><fileupload id="File1" runat="server" width="200px"/> <br/></contenttemplate> <br/><triggers> <br/><postbacktrigger controlid="Button1"/> <br/></triggers> <br/></updatepanel> <br/><image id="image1" imageurl="http://images.cnblogs.com/nopic.gif" height="115px" width="108px" runat="server"/> <br/></div> <br/></form> <br/> <br/> <br/> </code></pre> <br/> <strong>2) </strong> <br/> <pre><code> <br/>using System; <br/>using System.Collections.Generic; <br/>using System.Linq; <br/>using System.Web; <br/>using System.Web.UI; <br/>using System.Web.UI.WebControls; <br/>using System.IO; <br/>using System.Data.SqlClient; <br/>using System.Data; <br/>public partial class _Default:baseClass <br/>{ <br/>protected void Page_Load(object sender, EventArgs e) <br/>{ <br/>} <br/>protected void Button1_Click(object sender, EventArgs e) <br/>{ <br/>HttpPostedFile upFile = File1.PostedFile; <br/>int iFileLength = upFile.ContentLength; <br/>try <br/>{ <br/>if (iFileLength == 0) <br/>{ <br/>MessageBox(" !"); <br/>} <br/>else <br/>{ <br/>Byte[] FileByteArray = new Byte[iFileLength]; <br/>Stream StreamObject = upFile.InputStream; <br/>StreamObject.Read(FileByteArray, 0, iFileLength); <br/>SqlConnection conn = new SqlConnection("server=.;database=Test;uid=sa;pwd=1234;"); <br/>ExecuteBySQLNonQuery("delete from imageTable"); <br/>SqlCommand cmd = new SqlCommand("insert into [imageTable] values(@image)", conn); <br/>cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray; <br/>conn.Open(); <br/>cmd.ExecuteNonQuery(); <br/>conn.Close(); <br/>MessageBox(" !"); <br/>} <br/>image1.ImageUrl = "displayempphoto.ashx"; <br/>} <br/>catch (Exception ex) <br/>{ <br/>MessageBox(ex.Message); <br/>} <br/>} <br/>} <br/> </code></pre> <br/> <strong>3) displayempphoto.ashx</strong> <br/> <pre><code> <br/> <br/>using System; <br/>using System.Web; <br/>using System.Data.SqlClient; <br/>using System.Web.Configuration; <br/>using System.Data; <br/>public class DisplayEmpPhoto : IHttpHandler <br/>{ <br/>public void ProcessRequest(HttpContext context) <br/>{ <br/>using (SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["CONNECTIONSQL"].ConnectionString)) <br/>{ <br/>SqlCommand SQLCmd = cn.CreateCommand(); <br/>SQLCmd.CommandText = "SELECT imagedata FROM imageTable"; <br/>cn.Open(); <br/>using (SqlDataReader dr = SQLCmd.ExecuteReader(CommandBehavior.SingleRow)) <br/>{ <br/>if (dr.Read()) <br/>{ <br/>// HTTP , JPEG 。 <br/>context.Response.ContentType = "Image/JPEG"; <br/>context.Response.Clear(); <br/>context.Response.BufferOutput = true; <br/>context.Response.BinaryWrite(dr.GetSqlBytes(0).Value); <br/>} <br/>} <br/>} <br/>} <br/>public bool IsReusable <br/>{ <br/>get <br/>{ <br/>return false; <br/>} <br/>} <br/>} <br/> </code></pre> <div class="clearfix"> <span id="art_bot" class="jbTestPos"/> </div> </div> </div> </div>