asp.net UpdatePanel to achieve no refresh upload pictures _ Practical skills

Source: Internet
Author: User
1) Front Desk
Copy Code code 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 ">
<title></title>
<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= "Button1_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= "108px" runat= "server "/>
</div>
</form>
</body>

2) Backstage
Copy Code code 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 Button1_Click (object sender, EventArgs e)
{
Httppostedfile upfile = File1.PostedFile;
int ifilelength = Upfile.contentlength;
Try
{
if (ifilelength = 0)
{
MessageBox ("Please 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) Establish general processing document DISPLAYEMPPHOTO.ASHX
Copy Code code 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 ())
{
Changes the output format of the HTTP file header so that the browser knows that the file format being exported is a JPEG 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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.