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 复制代码

fileupload图片预览功能的更多相关文章

  1. 如何通过js实现图片预览功能

    一.效果预览 效果图: 二.实现代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  2. 原生JS实现图片预览功能

    html代码: <div class="album-new fr"> <div class="upload-btn btn-new container& ...

  3. HTML5实现图片预览功能

    两种方式实现 URL FileReader Index.jsp文件 <%@page contentType="text/html" pageEncoding="UT ...

  4. 原生js实现ajax的文件异步提交功能、图片预览功能.实例

    采用html5使得选择图片改变时,预览框中图片随之改变.input文件选择框美化.原生js完成文件异步提交 效果图: 代码如下,可直接复制并保存为html文件打开查看效果 <html> & ...

  5. H5图片预览功能

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. JavaScript图片上传前的图片预览功能

    JS代码: //js本地图片预览,兼容ie[6-9].火狐.Chrome17+.Opera11+.Maxthon3 function PreviewImage(fileObj, imgPreviewI ...

  7. 34)django-上传文件,图片预览功能实现

    目录 文件上传      1)form表单提交上传(会刷新)      2)ajax上传      3)iframe      4)图片上传预览(思路保存文件的时候,把文件保存文件的路径反馈回,客户端 ...

  8. 通过file文件选择图片预览功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. ext图片预览功能实现,前端代码

    效果图: extjs代码: // 模型 Ext.define('ParkingAttachment', {extend: "Ext.data.Model", idProperty: ...

随机推荐

  1. 个人作业-Week2 案例分析

    微软必应词典客户端的案例分析 第一部分 调研,评测 1)bug: 运行平台:iOS 10.0.2 必应词典版本:4.2.2 1. bug标题:词库加载错误 bug详细描述:学习界面中的经典词库出国考试 ...

  2. [TCPIP] 分层 Note

    TCP/IP  分层 TCP/IP是一组不同层次上的多个协议的组合. 通常被分为:链路层.网络层.运输层.应用层 1. 链路层(数据链路层 或 网络接口层) 通常包括操作系统中的设备驱动程序和计算机中 ...

  3. flume+sparkStreaming实例 实时监控文件demo

    1,flume所在的节点不和spark同一个集群  v50和 10-15节点 flume在v50里面 flume-agent.conf spark是开的work节点,就是单点计算节点,不涉及到mast ...

  4. c++多线程のunique和lazy initation

    unique更方便使用,但是会消耗更多的计算机性能 onceflag保证一个线程被调用一次,防止不能的加锁开锁

  5. 8.mvc core上传文件

    以下方法均是个人,仅供参考 public interface IFileHelper { /// <summary> /// 保存文件 (返回 Test.jpg) 出错就返回 error| ...

  6. python 初级1

    List:Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,按照上面的代码,直接用 [ ] 把list的所有元素都括起来, ...

  7. UVALive 3401 彩色立方体

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. Java方法trim()小记

    我们一般用trim()方法的主要作用,是为了去除字符串的首尾空格.然而根据我个人的实践经验发现,trim()这个方法只能去除部分的空格或空白符,比如半角空格:对于全角空格的话,用trim()并不能去除 ...

  9. Openfire Strophe开发中文乱码问题

    网站上有很多Openfire Web方案,之前想用Smack 但是jar包支持客户端版本的,还有JDK版本问题  一直没调试成功  估计成功的方法只能拜读源码进行修改了. SparkWeb 官网代码很 ...

  10. SQL基础--序列

    序列是一种数据库对象,用来自动产生一组唯一的序号:序列是一种共享式的对象,多个用户可以共同使用序列中的序号. 序列的创建语法 CREATE SEQUENCE sequencename [INCREME ...