发布于 2016-06-01 05:57:07 | 95 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


UpdatePanel实现无刷新上传图片实现代码,需要的朋友可以参考下。
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; 
} 
} 
} 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务