找了很多文章,当然也看不了不少,大部分的组件都要收费,而且有些就算破解了,也用起来不是很爽,比较以后部署来麻烦。终于找到一个不错上传控件---NeatUpload。更重要的是它是开源的。
演示地址:http://www.brettle.com/Demo.aspx
下载地址:http://www.brettle.com/neatupload#Download
1. 开始当然需要加入程序集的引用,这里应用有两个:Brettle.Web.NeatUpload.dll,Brettle.Web.NeatUpload.HashedInputFile.dll。
2. 需要在页面端加入注册:
<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload" Assembly="Brettle.Web.NeatUpload" %>
3. 页面中加入(第一个上传控件,第二个是上传状态栏,里面的Triggers绑定某个Button)
<Upload:InputFile ID="inputFile" runat="server" />
<Upload:ProgressBar id="inlineProgressBar" runat="server" inline="true" Triggers="btnUpload " Height="67px" />
<asp:Button ID="btnUpload" runat="server" Text="Button" OnClick="btnUpload_Click" />
4. 在btnUpload_Click加入代码:
if (!this.IsValid)
{
bodyPre.InnerText = "Page is not valid!";
return;
}
bodyPre.InnerText = "";
if (inputFile.HasFile)
{
inputFile.MoveTo(Path.Combine(Request.PhysicalApplicationPath + @"\download", inputFile.FileName),
MoveToOptions.Overwrite);
}
(最关键的是MoveTo函数)
5. 需要在Web.Config里面加入一些设置:
1)在<configuration>里面加入
<configSections>
<sectionGroup name="system.web">
<section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler, Brettle.Web.NeatUpload" allowLocation="true" />
</sectionGroup>
</configSections>
2) 在 <system.web>加入
<neatUpload useHttpModule="false" maxNormalRequestLength="4096" maxRequestLength="2097151" defaultProvider="FilesystemUploadStorageProvider">
<providers>
<add name="FilesystemUploadStorageProvider"
type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload"
/>
</providers>
</neatUpload>
3)在<configuration>里面的后面位置加入
<location path="Default.aspx" >
<system.web>
<neatUpload useHttpModule="true" />
<httpRuntime maxRequestLength="2097151" executionTimeout="3600" useFullyQualifiedRedirectUrl="true" />
</system.web>
</location>
</configuration>
4)为了使ProgreessBAR能显示,在配置文件<system.web>中需要加入
<httpRuntime useFullyQualifiedRedirectUrl="true" />
<httpModules>
<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule, Brettle.Web.NeatUpload" />
</httpModules>
同时需要建立一个NeatUpload文件架,把例子中该文件夹的东西Copy进去。
修改后的Progress.aspx页面(把英文改成中文,改了点样式)
<%@ Page language="c#" AutoEventWireup="false" Inherits="Brettle.Web.NeatUpload.ProgressPage" %>
<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload" Assembly="Brettle.Web.NeatUpload" %>
<%--
NeatUpload - an HttpModule and User Controls for uploading large files
Copyright (C) 2005 Dean Brettle
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--%>
<html>
<head>
<title>Upload Progress</title>
<link rel="stylesheet" type="text/css" title="default" href="default.css" />
<style type="text/css">
<!--
body, form, table, tr, td {
margin: 0px;
border: 0px none;
padding: 0px;
}
#progressDisplayCenterer {
vertical-align: middle;
width: 100%;
height: 100%;
}
#progressDisplay {
vertical-align: middle;
width: 100%;
}
#barTd {
width: 100%;
}
#statusDiv {
border-width: 1px;
border-style: solid;
padding: 0px;
position: relative;
width: 100%;
text-align: center;
z-index: 1;
}
#barDiv,#barDetailsDiv {
border: 0px none ;
margin: 0px;
padding: 0px;
position: absolute;
top: 0pt;
left: 0pt;
z-index: -1;
height: 100%;
width: 75%;
}
-->
</style>
</head>
<body>
<form id="dummyForm" runat="server">
<table id="progressDisplayCenterer">
<tr>
<td>
<span id="label" runat="server" class="Label">上传进度:</span>
<table id="progressDisplay" class="ProgressDisplay">
<tr>
<td>
</td>
<td id="barTd" >
<div id="statusDiv" runat="server" class="StatusMessage"> 
<Upload:DetailsSpan id="normalInProgress" runat="server" WhenStatus="NormalInProgress" style="font-weight: normal; white-space: nowrap;">
<%# FormatCount(BytesRead) %>/<%# FormatCount(BytesTotal) %> <%# CountUnits %>
(<%# String.Format("{0:0%}", FractionComplete) %>) -- <%# FormatRate(BytesPerSec) %>
-剩余 <%# FormatTimeSpan(TimeRemaining) %>
</Upload:DetailsSpan>
<Upload:DetailsSpan id="chunkedInProgress" runat="server" WhenStatus="ChunkedInProgress" style="font-weight: normal; white-space: nowrap;">
<%# FormatCount(BytesRead) %> <%# CountUnits %>
at <%# FormatRate(BytesPerSec) %>
- <%# FormatTimeSpan(TimeElapsed) %> elapsed
</Upload:DetailsSpan>
<Upload:DetailsSpan id="completed" runat="server" WhenStatus="Completed">
完成: <%# FormatCount(BytesRead) %> <%# CountUnits %>
-- <%# FormatRate(BytesPerSec) %>
耗时 <%# FormatTimeSpan(TimeElapsed) %>
</Upload:DetailsSpan>
<Upload:DetailsSpan id="cancelled" runat="server" WhenStatus="Cancelled">
取消!
</Upload:DetailsSpan>
<Upload:DetailsSpan id="rejected" runat="server" WhenStatus="Rejected">
拒绝 <%# Rejection != null ? Rejection.Message : "" %>
</Upload:DetailsSpan>
<Upload:DetailsSpan id="error" runat="server" WhenStatus="Failed">
错误: <%# Failure != null ? Failure.Message : "" %>
</Upload:DetailsSpan>
<Upload:DetailsDiv id="barDetailsDiv" runat="server" UseHtml4="true"
Width='<%# Unit.Percentage(Math.Floor(100*FractionComplete)) %>' class="ProgressBar"></Upload:DetailsDiv>
</div>
</td>
<td>
<asp:HyperLink id="cancel" runat="server" Visible='<%# CancelVisible %>' NavigateUrl='<%# CancelUrl %>' title="Cancel Upload" class="ImageButton" ><img id="cancelImage" runat="server" src="cancel.png" alt="Cancel Upload" /></asp:HyperLink>
<asp:HyperLink id="refresh" runat="server" Visible='<%# StartRefreshVisible %>' NavigateUrl='<%# StartRefreshUrl %>' title="Refresh" class="ImageButton" ><img id="refreshImage" runat="server" src="refresh.png" alt="Refresh" /></asp:HyperLink>
<asp:HyperLink id="stopRefresh" runat="server" Visible='<%# StopRefreshVisible %>' NavigateUrl='<%# StopRefreshUrl %>' title="Stop Refreshing" class="ImageButton"><img id="stopRefreshImage" runat="server" src="stop_refresh.png" alt="Stop Refreshing" /></asp:HyperLink>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
还在修改中。。。。。