Look at the effect:
In the project, create an ASPX page that pulls an image of the FileUpload control to preview the image when it is uploaded.
Copy Code code as follows:
View Code
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default2.aspx.cs" inherits= "DEFAULT2"%>
<! DOCTYPE html>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<table>
<tr>
<TD style= "Vertical-align:top; width:10%; " >
<fieldset>
<legend> Choose Pictures </legend>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
</fieldset>
</td>
<TD style= "Vertical-align:top; width:90%; " >
<fieldset>
<legend> Preview </legend>
<asp:image id= "Image1" runat= "Server" visible= "false"/>
</fieldset>
</td>
</tr>
</table>
</div>
</form>
</body>
In the Page_Init event, for the FileUpload control, register the onchange client event.
Copy Code code as follows:
protected void Page_Init (object sender, EventArgs e)
{
This. FILEUPLOAD1.ATTRIBUTES.ADD ("onchange", Page.ClientScript.GetPostBackEventReference) (this. FileUpload1, "onchange"));
}
Next, insus.net a axd processing document, in fact ImageProcessFactory.cs is only a Ponnen category, only implemented the IHttpHandler interface.
Copy Code code as follows:
ImageProcessFactory.cs
Using System;
Using System.Collections.Generic;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Drawing.Imaging;
Using System.IO;
Using System.Linq;
Using System.Web;
Using System.Web.SessionState;
<summary>
Summary description for Imageprocessfactory
</summary>
Namespace Insus.net
{
public 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;
}
}
}
}
To be able to axd documents, you need to configure them in Web.config.
Copy Code code as follows:
View Code
<configuration>
<system.web>
<add verb= "*" path= "Previewimage.axd" type= "Insus.NET.ImageProcessFactory"/>
</system.web>
</configuration>
Ok, let's go back to the Aspx.cs page, and in Page_Load, how to monitor the FileUpload control for value changes:
Copy Code code as follows:
View Code
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack)
{
var ctrl = Request.params[page.posteventsourceid];
var args = Request.params[page.posteventargumentid];
Onchangehandle (ctrl, args);
}
}
There is a method in the Page_Load Onchangehandle (xxx,xxx):
Copy Code code as follows:
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";
}
}