View Code
<%@ Page Language="C
#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inher
ITs="Default2" %>
<!DOCTY
PE ht
ML>
<html
XMlns="http://www.w3.
org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="vert
ical-align: top; width: 10%;">
<fieldset>
<legend>选择图片</legend>
<asp:FileUpload ID="FileUpload1" runat="server" />
</fieldset>
</td>
<td style="vertical-align: top; width: 90%;">
<fieldset>
<legend>预览</legend>
<asp:Image ID="Image1" runat="server" Visible="false" />
</fieldset>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
PRotected void Page_Init(object sender, Event
args e)
{
this.FileUpload1.Attributes.Add("onchange", Page.ClientScript.GetPostBackEventReference(this.FileUpload1, "onchange"));
}
ImageProcessFactory.cs
using
System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Ima
ging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Session
state;
/// <summary>
/// Summary description for ImageProcessFactory
/// </summary>
namespace Insus.NET
{
p
ublic class ImageProcessFactory : IHttpHandler,IRequiresSessionState
{
public ImageProcessFactory()
{
//
// TODO: Add constructor
LOGic here
//
}
public void ProcessRequest(HttpContext context)
{
//Checking whether the UploadBytes session
VARiable have anything else not doing anything
if ((context.Session["UploadBytes"]) != null)
{
byte[] buffer = (byte[])(context.Session["UploadBytes"]);
context.Response.BinaryWrite(buffer);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
为能能应到axd文档,需要在Web.
View Code
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="P
reviewImage.axd" type="Insus.NET.ImageProcessFactory"/>
</httpHandlers>
</system.web>
</configuration>
View Code
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var ctrl = Request.Par
ams[Page.postEventSourc
eiD];
var args = Request.Params[Page.postEvent
argumentID];
OnchangeHandle(ctrl, args);
}
}
View Code
private void OnchangeHandle(string ctrl, string args)
{
if (ctrl
== this.FileUpload1.
uniqueID
&& args == "onchange")
{
this.Image1.Visible = true;
Session["UploadBytes"] = this.FileUpload1.FileBytes;
this.Image1.ImageUrl = "~/PreviewImage.axd" ;
}
}