FileUpload upload pictures before the implementation of the image Preview function (with demo animation) _ Practical skills

Source: Internet
Author: User
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>
&LT;TD style= "Vertical-align:top; width:10%; " >
<fieldset>
<legend> Choose Pictures </legend>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
</fieldset>
</td>
&LT;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";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.