最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

FileUpload上傳圖片前實(shí)現(xiàn)圖片預(yù)覽功能(附演示動(dòng)畫(huà))

 更新時(shí)間:2013年01月10日 17:18:01   作者:  
FileUpload控件上傳圖片前實(shí)現(xiàn)預(yù)覽,很多網(wǎng)友都希望實(shí)現(xiàn)這樣的功能,本人總結(jié)了一下,感興趣的朋友可以參考一下,希望對(duì)您有幫助
看看效果:
 
在專(zhuān)案中,創(chuàng)建aspx頁(yè)面,拉上FileUpload控件一個(gè)Image,將用來(lái)預(yù)覽上傳時(shí)的圖片。
復(fù)制代碼 代碼如下:

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>預(yù)覽</legend>
<asp:Image ID="Image1" runat="server" Visible="false" />
</fieldset>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

在Page_Init事件中,為FileUpload控件,注冊(cè)onchange客戶(hù)端事件。
復(fù)制代碼 代碼如下:

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

接下來(lái),Insus.NET一個(gè)axd處理文檔,其實(shí)ImageProcessFactory.cs只是一個(gè)普能的類(lèi)別,只實(shí)作了IHttpHandler接口。
復(fù)制代碼 代碼如下:

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

為能能應(yīng)到axd文檔,需要在Web.Config中配置一下。
復(fù)制代碼 代碼如下:

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

Ok,我們回到aspx.cs頁(yè)面中,要在page_Load中,怎監(jiān)控FileUpload控件是否有值變化:
復(fù)制代碼 代碼如下:

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中有一個(gè)方法OnchangeHandle(xxx,xxx):
復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評(píng)論

鹿邑县| 星座| 洛川县| 延长县| 阿坝| 乐东| 阳泉市| 蒲江县| 临西县| 汕头市| 英吉沙县| 武穴市| 黎川县| 荔波县| 吕梁市| 麻城市| 建水县| 宣武区| 苏尼特右旗| 望奎县| 息烽县| 竹北市| 枞阳县| 左贡县| 杭州市| 廉江市| 土默特左旗| 潼关县| 林口县| 九寨沟县| 稻城县| 顺平县| 巨鹿县| 互助| 阿合奇县| 清原| 谢通门县| 嘉定区| 建宁县| 固始县| 福清市|