FileUpload上傳圖片前實(shí)現(xiàn)圖片預(yù)覽功能(附演示動(dòng)畫)
更新時(shí)間:2013年01月10日 17:18:01 作者:
FileUpload控件上傳圖片前實(shí)現(xiàn)預(yù)覽,很多網(wǎng)友都希望實(shí)現(xiàn)這樣的功能,本人總結(jié)了一下,感興趣的朋友可以參考一下,希望對(duì)您有幫助
看看效果:
在專案中,創(chuàng)建aspx頁(yè)面,拉上FileUpload控件一個(gè)Image,將用來(lái)預(yù)覽上傳時(shí)的圖片。
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客戶端事件。
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è)普能的類別,只實(shí)作了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;
}
}
}
}
為能能應(yīng)到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頁(yè)面中,要在page_Load中,怎監(jiān)控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中有一個(gè)方法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" ;
}
}
在專案中,創(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客戶端事件。
復(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è)普能的類別,只實(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" ;
}
}
您可能感興趣的文章:
- 基于jquery實(shí)現(xiàn)的上傳圖片及圖片大小驗(yàn)證、圖片預(yù)覽效果代碼
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- js 上傳圖片預(yù)覽問題
- javascript IE7 瀏覽器本地圖片預(yù)覽
- 純JS實(shí)現(xiàn)的批量圖片預(yù)覽加載功能
- JavaScript 圖片預(yù)覽效果 推薦
- 兼容最新firefox、chrome和IE的javascript圖片預(yù)覽實(shí)現(xiàn)代碼
- 手機(jī)圖片預(yù)覽插件photoswipe.js使用總結(jié)
- 移動(dòng)端點(diǎn)擊圖片放大特效PhotoSwipe.js插件實(shí)現(xiàn)
相關(guān)文章
.NET?Core配置連接字符串和獲取數(shù)據(jù)庫(kù)上下文實(shí)例
這篇文章介紹了.NET?Core配置連接字符串和獲取數(shù)據(jù)庫(kù)上下文實(shí)例的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
獲取客戶端IP地址c#/vb.net各自實(shí)現(xiàn)代碼
項(xiàng)目要求獲取客戶端的IP地址,分析并且用c#與vb各自實(shí)現(xiàn)了此要求,有需要的朋友可以了解下啊,希望本文對(duì)你們有所幫助2013-01-01
Asp.net中判斷一個(gè)session是否合法的方法
今天突然想到一個(gè)判斷session是否合法的做法,asp.net的,之前我們的做法是下面這樣的形式的:2013-07-07
VS2015自帶LocalDB數(shù)據(jù)庫(kù)用法詳解
這篇文章主要為大家詳細(xì)介紹了VS2015自帶LocalDB數(shù)據(jù)庫(kù)的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET 鏈接 Access 數(shù)據(jù)庫(kù)路徑問題最終解決方案
ASP.NET 鏈接 Access 數(shù)據(jù)庫(kù)路徑問題最終解決方案...2007-04-04
ASP.NET Web應(yīng)用程序出現(xiàn)Maximum request length
ASP.NET Web應(yīng)用中導(dǎo)出數(shù)據(jù)時(shí)出現(xiàn)500-Internal Server Error,原因是客戶端請(qǐng)求長(zhǎng)度超過(guò)了服務(wù)器配置的最大限制,解決方法在web.config增加maxRequestLength屬性,單位為字節(jié)(Byte),本文介紹ASP.NET Web應(yīng)用程序出現(xiàn)Maximum request length exceeded報(bào)錯(cuò)的原因,一起看看吧2024-12-12
ASP.NET中利用Segments取得URL的文件名的一種方法分享
在ASP.NET中,取得請(qǐng)求頁(yè)的URL地址有多種方式,其中有一種方式取得網(wǎng)頁(yè)文件名。2011-09-09

