asp.net UpdatePanel實(shí)現(xiàn)無刷新上傳圖片
更新時間:2010年03月05日 18:15:05 作者:
UpdatePanel實(shí)現(xiàn)無刷新上傳圖片實(shí)現(xiàn)代碼,需要的朋友可以參考下。
1)前臺
<%@ 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)后臺
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("請選擇要上傳的文件!");
}
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)后臺
復(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("請選擇要上傳的文件!");
}
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)無組件上傳圖片實(shí)例介紹
- asp.net+FCKeditor上傳圖片顯示叉叉圖片無法顯示的問題的解決方法
- ASP.NET下上傳圖片到數(shù)據(jù)庫,并且讀出圖片的代碼(詳細(xì)版)
- asp.net上傳圖片保存到數(shù)據(jù)庫的代碼
- asp.net 自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- ASP.NET FileUpload 上傳圖片實(shí)例
- asp.net上傳圖片到服務(wù)器方法詳解
相關(guān)文章
詳解.Net Core + Angular2 環(huán)境搭建
這篇文章主要介紹了詳解.Net Core + Angular2 環(huán)境搭建,具有一定的參考價值,有興趣的可以了解一下。2016-12-12ASP.NET MVC中使用Bundle打包壓縮js和css的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC中使用Bundle打包壓縮js和css的方法,感興趣的小伙伴們可以參考一下2016-05-05ADO.NET中的五個主要對象的詳細(xì)介紹與應(yīng)用
ADO.NET中的五個主要對象:Connection、Command、DataAdapter DataSet、DataReader詳細(xì)介紹與應(yīng)用,感興趣的朋友可以參考下2012-12-12FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
在ASP.Net 2.0中使用,只需要2個文件:FreeTextBox.DLL和ftb.imagegallery.aspx2009-11-11asp.net Reporting Service在Web Application中的應(yīng)用
由于我們這個項(xiàng)目中使用微軟的報(bào)表服務(wù)(Reporting Services)作為報(bào)表輸出工具,本人也對它進(jìn)行一點(diǎn)點(diǎn)研究,雖沒有入木三分,但這點(diǎn)知識至少可以在大部分Reporting Service的場景中應(yīng)用。2008-11-11簡單使用BackgroundWorker創(chuàng)建多個線程的教程
簡單使用BackgroundWorker創(chuàng)建多個線程的教程,需要的朋友可以參考一下2013-03-03