FileUpload上传图片前首先预览一下

看看效果:

在专案中,创建aspx页面,拉上FileUpload控件一个Image,将用来预览上传时的图片。

<%@ 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接口。

按 Ctrl+C 复制代码

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

按 Ctrl+C 复制代码

为能应用到axd文档,需要在Web.Config中配置一下。

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

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

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):

按 Ctrl+C 复制代码

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

按 Ctrl+C 复制代码

最新文章

  1. 41、javaMail机制
  2. java 读取pdf、word、Excel文件
  3. 清爽绿色格调图文box通用样式
  4. ASP.NET连接Oracle数据库的步骤详解(转)
  5. phpmailer 发送邮件
  6. LitJson处理Json
  7. ⑤bootstrap表格使用基础案例
  8. 分组PARTITION BY及游标CURSOR的用法
  9. CSS关键词的值-currentColor关键字提示文字(当前颜色)
  10. cmd 安装mysql
  11. [Robot Framework] 调用ExcelLibrary
  12. 在ASP.NET MVC应用程序中随机获取一个字符串
  13. 洛谷P2605 基站选址
  14. 如何通过Node.js启动cesium
  15. js验证两次输入的密码是否一致
  16. Unity即将内置骨骼动画插件Anima2D
  17. ref:CodeIgniter框架内核设计缺陷可能导致任意代码执行
  18. Hadoop集群搭建文档
  19. Kafka学习总结
  20. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

热门文章

  1. Javascript &gt; Eclipse &gt; problems encountered during text search
  2. C++Builder XE8_upd1破解安装成功纪要
  3. Boost学习笔记(五) progress_display
  4. Hadoop 简介
  5. iOS改变字母的大小写
  6. Java-输入输出流
  7. UE4 使用UGM制作血条
  8. Fiddler 4 抓包
  9. Gbase数据库备份与还原
  10. git备份sublime插件及配置