1) Front-end
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server" UpdateMode = "Conditional">
<ContentTemplate>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/>
<Asp: FileUpload ID = "File1" runat = "server" Width = "200px"/>
</ContentTemplate>
<Triggers>
<Asp: PostBackTrigger ControlID = "Button1"/>
</Triggers>
</Asp: UpdatePanel>
<Asp: Image id = "image1" ImageUrl = "http://images.cnblogs.com/nopic.gif" Height = "115px" Width = "pixel PX" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
2) Background
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using System. Data. SqlClient;
Using System. Data;
Public partial class _ Default: baseClass
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
HttpPostedFile upFile = File1.PostedFile;
Int iFileLength = upFile. ContentLength;
Try
{
If (iFileLength = 0)
{
MessageBox ("select the file to upload! ");
}
Else
{
Byte [] FileByteArray = new Byte [iFileLength];
Stream StreamObject = upFile. InputStream;
StreamObject. Read (FileByteArray, 0, iFileLength );
SqlConnection conn = new SqlConnection ("server = ..; database = Test; uid = sa; pwd = 1234 ;");
ExecuteBySQLNonQuery ("delete from imageTable ");
SqlCommand cmd = new SqlCommand ("insert into [imageTable] values (@ image)", conn );
Cmd. Parameters. Add ("@ Image", SqlDbType. Binary, iFileLength). Value = FileByteArray;
Conn. Open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();
MessageBox ("the photo has been uploaded successfully! ");
}
Image1.ImageUrl = "displayempphoto. ashx ";
}
Catch (Exception ex)
{
MessageBox (ex. Message );
}
}
}
3) create displayempphoto. ashx
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "DisplayEmpPhoto" %>
Using System;
Using System. Web;
Using System. Data. SqlClient;
Using System. Web. Configuration;
Using System. Data;
Public class DisplayEmpPhoto: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
Using (SqlConnection cn = new SqlConnection (WebConfigurationManager. ConnectionStrings ["CONNECTIONSQL"]. ConnectionString ))
{
SqlCommand SQLCmd = cn. CreateCommand ();
SQLCmd. CommandText = "SELECT imagedata FROM imageTable ";
Cn. Open ();
Using (SqlDataReader dr = SQLCmd. ExecuteReader (CommandBehavior. SingleRow ))
{
If (dr. Read ())
{
// Change the output format of the HTTP File Header so that the browser can know that the output file format is a JPEG image file.
Context. Response. ContentType = "Image/JPEG ";
Context. Response. Clear ();
Context. Response. BufferOutput = true;
Context. Response. BinaryWrite (dr. GetSqlBytes (0). Value );
}
}
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}