欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net UpdatePanel实现无刷新上传图片

程序员文章站 2024-03-08 08:35:09
1)前台 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="default.asp...
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("已经成功上传了照片!");
}
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;
}
}
}