asp.net UpdatePanel實(shí)現(xiàn)無(wú)刷新上傳圖片
更新時(shí)間:2010年03月05日 18:15:05 作者:
UpdatePanel實(shí)現(xiàn)無(wú)刷新上傳圖片實(shí)現(xiàn)代碼,需要的朋友可以參考下。
1)前臺(tái)
<%@ 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="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>
</html>
2)后臺(tái)
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("請(qǐng)選擇要上傳的文件!");
}
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("已經(jīng)成功上傳了照片!");
}
image1.ImageUrl = "displayempphoto.ashx";
}
catch (Exception ex)
{
MessageBox(ex.Message);
}
}
}
3)建立一般處理文件displayempphoto.ashx
<%@ 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())
{
// 改變 HTTP 文件頭的輸出格式,以便讓瀏覽器知道所輸出的文件格式是 JPEG 圖文件。
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;
}
}
}
復(fù)制代碼 代碼如下:
<%@ 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="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>
</html>
2)后臺(tái)
復(fù)制代碼 代碼如下:
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("請(qǐng)選擇要上傳的文件!");
}
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("已經(jīng)成功上傳了照片!");
}
image1.ImageUrl = "displayempphoto.ashx";
}
catch (Exception ex)
{
MessageBox(ex.Message);
}
}
}
3)建立一般處理文件displayempphoto.ashx
復(fù)制代碼 代碼如下:
<%@ 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())
{
// 改變 HTTP 文件頭的輸出格式,以便讓瀏覽器知道所輸出的文件格式是 JPEG 圖文件。
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;
}
}
}
您可能感興趣的文章:
- asp.net fileupload控件上傳圖片并預(yù)覽圖片
- ASP.net WebAPI 上傳圖片實(shí)例
- asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
- asp.net MVC實(shí)現(xiàn)無(wú)組件上傳圖片實(shí)例介紹
- asp.net+FCKeditor上傳圖片顯示叉叉圖片無(wú)法顯示的問(wèn)題的解決方法
- ASP.NET下上傳圖片到數(shù)據(jù)庫(kù),并且讀出圖片的代碼(詳細(xì)版)
- asp.net上傳圖片保存到數(shù)據(jù)庫(kù)的代碼
- asp.net 自定義控件實(shí)現(xiàn)無(wú)刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- ASP.NET FileUpload 上傳圖片實(shí)例
- asp.net上傳圖片到服務(wù)器方法詳解
相關(guān)文章
ASP.net如何連接SQL SERVER 2012數(shù)據(jù)庫(kù)
這篇文章主要介紹了ASP.net連接SQL SERVER 2012數(shù)據(jù)庫(kù)的方法,非常不錯(cuò),在項(xiàng)目開(kāi)發(fā)中經(jīng)??梢杂玫?,需要的朋友可以參考下2016-08-08
ASP.NET簡(jiǎn)單實(shí)現(xiàn)注銷功能
這篇文章主要介紹了如何ASP.NET簡(jiǎn)單實(shí)現(xiàn)注銷功能的方法以及簡(jiǎn)單示例,有需要的小伙伴可以參考下。2015-07-07
asp.net core項(xiàng)目授權(quán)流程詳解
本文詳細(xì)講解了asp.net core項(xiàng)目的授權(quán)流程,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
禁用aspx頁(yè)面的客戶端緩存(防止頁(yè)面被修改)
默認(rèn)情況下,IE打開(kāi)一個(gè)網(wǎng)頁(yè),會(huì)在本地進(jìn)行緩存,在某些時(shí)候也會(huì)帶來(lái)了弊端,比如修改信息的頁(yè)面等等因?yàn)閁RL并沒(méi)有改變,所以IE會(huì)讀取本地緩存,這種情況特別容易出現(xiàn)在彈出對(duì)話框或窗口進(jìn)行修改的方式感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02
ASP.NET Core MVC學(xué)習(xí)之視圖組件(View Component)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC學(xué)習(xí)之視圖組件(View Component)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
.NET?中的線程安全數(shù)據(jù)結(jié)構(gòu)詳解
.NET提供了多種線程安全的數(shù)據(jù)結(jié)構(gòu),適用于不同的場(chǎng)景,本篇將介紹它們的簡(jiǎn)單使用以及在.NETCore和.NET Framework中的可用性,感興趣的朋友一起看看吧2024-12-12
數(shù)據(jù)庫(kù)開(kāi)發(fā)總結(jié)(ADO.NET小結(jié))
數(shù)據(jù)庫(kù)開(kāi)發(fā)總結(jié)(ADO.NET小結(jié))...2006-12-12

