FileUpload上传图片前实现图片预览功能(附演示动画)

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了FileUpload上传图片前实现图片预览功能(附演示动画)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
看看效果:
 
在专案中,创建aspx页面,拉上FileUpload控件一个Image,将用来预览上传时的图片。
复制代码 代码如下:

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" InherITs="Default2" %>
<!DOCTYPE htML>
<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="vertical-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>

在Page_Init事件中,为FileUpload控件,注册onchange客户端事件。
复制代码 代码如下:

PRotected void Page_Init(object sender, Eventargs e)
{
this.FileUpload1.Attributes.Add("onchange", Page.ClientScript.GetPostBackEventReference(this.FileUpload1, "onchange"));
}

接下来,Insus.NET一个axd处理文档,其实ImageProcessFactory.cs只是一个普能的类别,只实作了IHttpHandler接口。
复制代码 代码如下:

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;
}
}
}
}

为能能应到axd文档,需要在Web.config中配置一下。
复制代码 代码如下:

View Code
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="PreviewImage.axd" type="Insus.NET.ImageProcessFactory"/>
</httpHandlers>
</system.web>
</configuration>

Ok,我们回到aspx.cs页面中,要在page_Load中,怎监控FileUpload控件是否有值变化:
复制代码 代码如下:

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);
}
}

在Page_Load中有一个方法OnchangeHandle(xxx,xxx):
复制代码 代码如下:

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" ;
}
}

脚本宝典总结

以上是脚本宝典为你收集整理的FileUpload上传图片前实现图片预览功能(附演示动画)全部内容,希望文章能够帮你解决FileUpload上传图片前实现图片预览功能(附演示动画)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。