分享:Asp.Net上传组件RadUpLoad(带进度条时间显示)(转)
在我的企业管理系统开发的过程中,遇到的最大问题就是大文件的上传了。在asp或者php中要想上传大文件就需借助于组件(这会受到虚拟主机的限制)。但是在asp.net中大文件上传似乎要简单了很多。下面提供下asp.net大文件上传的解决方案。
贴上代码,做.net开发的应该一看就知道什么意思
1.btnSave保存文件提交按钮事件
/// <summary>
/// btnSave保存文件提交按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
{
string Path = Server.MapPath(@"Uploads");//如果路径不存在,则创建
if (System.IO.Directory.Exists(Path) == false)
{
System.IO.Directory.CreateDirectory(Path);
}//组合路径,file.GetName()取得文件名
Path = Path + "/" + file.GetName().ToString();//保存
file.SaveAs(Path, true);
}
}
此处代码要在上面增加 using Telerik.WebControls;
2. 前台代码 顶部注册组件
<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" Assembly="Telerik.QuickStart" %>
<%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.Net2" %
3. Html页面
<radu:radprogressmanager id="Radprogressmanager1" Width="100%" runat="server" Height="37px" />
<radu:radprogressarea id="progressArea1" Width="100%" runat="server"></radu:radprogressarea>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
4.引用DLL为RadUpload.Net2.dll与 Telerik.QuickStart.dll ,RadUpload.Net2.xml都在BIN目录下
5. webconfig配置
<httpModules>
<add name=
"RadUploadModule"
type=
"Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"
/>
</httpModules>
<httpHandlers>
<add verb=
"*"
path=
"Telerik.RadUploadProgressHandler.aspx"
type=
"Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"
></add>
</httpHandlers>
<!--此处必须有要httpRuntime配置,否则报DNSerror,而不能上传-->
<httpRuntime maxRequestLength=
"2097151"
executionTimeout=
"3600"
/>
2.引用并声明使用
<%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.NET2" %>
<radU:RadProgressManager ID="Radprogressmanager1" runat="server" />
<radU:RadUpload ID="RadUpload1" runat="server" Skin="Web20" InitialFileInputsCount="2"
MaxFileInputsCount="4" AllowedFileExtensions=".doc,.rar" MaxFileSize="10000000"
TargetFolder="~/Upload/kjcx" verwriteExistingFiles="false" />
<asp:Button ID="buttonSubmit" runat="server" CssClass="RadUploadButton" Text="Submit!" />
<radU:RadProgressArea ID="progressArea1" runat="server" />
很多参数的设置参考 :http://www.telerik.com/demos/aspnet/Upload/Examples/Overview/DefaultCS.aspx
3.文件大小的控制,如果太大会出现页面报错。
web.config
<httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="8192" requestLengthDiskThreshold="8192" />
报错的帮助文档:
http://www.telerik.com/support/kb/article/b454K-gth-b454T-cee.aspx
http://www.telerik.com/help/radUpload/v2%5FNet2/?UploadingLargeFiles.html
rad control的文档比较少,只有官方的,自己摸索吧