ASP.NET2.0:Ilungasoft.Framework.Web之基于Callback的无刷新上传进度条控件[带源码]

来源:互联网 发布:英语教学软件 编辑:程序博客网 时间:2024/05/29 03:11
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
共享一个基于Callback的无刷新上传进度条控件的源码。本控件使用的HttpMoudule基于宝玉的一个上传进度条的sample,这里封装为一个控件,方便使用。无需任何代码,只需设置Web.config,添加HttpModule的引用,再将控件拖到页面就行。页面中的文件保存操作和传统的ASP.NET文件上传完全一样。可以设置属性上传过程中出错或上传成功时跳转到其它页面。兼容IE,Firefox,Opera。其它环境没测试,不过因为是基于ASP.NET2.0的Callback,其他浏览器只要支持xmlhttp或iframe就应该支持。

在线演示请访问:http://teddy.cn/test

源码及示例下载http://teddyma.cnblogs.com/Files/teddyma/TestUploadProgressBar.zip

(在本机运行示例注意将程序所在目录设为对Web帐号可写,否则上传文件是会权限不足报错)

下面简单列举一下示例中的Web.config和Default.ASPx和Default.ASPx.cs。

Web.config

1<?xml version="1.0"?>
2<configuration>
3 <appSettings/>
4 <connectionStrings/>
5 <system.Web>
6 <compilation debug="true"/>
7 <authentication mode="Windows"/>
8 <httpModules>
9 <add name="HttpUploadModule" type="Ilungasoft.Framework.Web.Modules.UploadProgressModule, Framework.Web"/>
10 </httpModules>
11 <httpRuntime maxRequestLength="1000000" executionTimeout="300"/>
12 </system.Web>
13</configuration>
Default.ASPx (注意Line 17必须设置控件的UploadButtonName为页面中出发上传事件的按钮的ID)

1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" %>
2
3<%@ Register Assembly="Framework.Web" Namespace="Ilungasoft.Framework.Web.UI.WebControls"
4 TagPrefix="cc1" %>
5<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6<html xmlns="http://www.w3.org/1999/xhtml">
7<head id="Head1" runat="server">
8 <title>Untitled Page</title>
9</head>
10<body>
11 <form id="form1" runat="server">
12 <div>
13 <ASP:FileUpload ID="FileUpload1" runat="server" /><br />
14 <br />
15 <ASP:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
16 <br />
17 <cc1:UploadProgressBar ID="UploadProgressBar1" runat="server" UploadButtonName="Button1" UploadErrorRedirectUrl="UploadError.ASPx">
18 </cc1:UploadProgressBar>
19 &nbsp;&nbsp;<br />
20 <br />
21 </div>
22 </form>
23</body>
24</html>
Default.ASPx.cs

1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11public partial class _Default : System.Web.UI.Page
12{
13 protected void Page_Load(object sender, EventArgs e)
14 {
15
16 }
17 protected void Button1_Click(object sender, EventArgs e)
18 {
19 FileUpload1.SaveAs(Server.MapPath("test.tmp"));
20 }
21}
是不是没感觉到和使用该控件之前相比多了任何代码呢?;-)

Enjoy!

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>