共享一個基于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 <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!
新聞熱點
疑難解答
圖片精選