ASP上传大文件组件

在ASP.NET开发过程中,文件上传是一个常见需求,ASP.NET自带的FileUpload控件存在诸多限制,特别是在处理大文件时,开发者经常感到头疼,为了解决这一问题,推荐使用一些第三方的大文件上传组件,本文将详细介绍几款常见的ASP.NET大文件上传组件及其使用方法。
一、Telerik RadUpload for ASP.NET AJAX
1. 简介
Telerik RadUpload是一款功能强大的ASP.NET AJAX上传组件,支持多线程操控和上传进度显示,它能够有效解决大文件上传的问题。
2. 安装与配置
下载组件:从官方网站下载RadUpload组件。
添加到项目:将下载的两个dll文件添加到项目的引用中,并将xml文件复制到bin文件夹下。
Web.config配置:
<httpRuntime executionTimeout="3600" maxRequestLength="2097151" />
<httpModules>
<add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"/>
</httpModules>
<httpHandlers>
<add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"/>
</httpHandlers>前台代码:

<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" Assembly="Telerik.QuickStart" %>
<%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.Net2" %>
<form id="form1" runat="server">
<asp:FileUpload runat="server" ID="fileUPload" />
<radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />
<radU:RadProgressArea ID="progressArea1" Width="100%" runat="server"></radU:RadProgressArea>
<asp:Button ID="fileUpladBtn" runat="server" Text="上传" OnClick="fileUpladBtn_OnClick" />
</form>后台代码:
protected void fileUpladBtn_OnClick(object sender, EventArgs e)
{
if (RadUploadContext.Current == null) { return; }
if (RadUploadContext.Current.UploadedFiles.Count <= 0)
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('请选择上传文件 !')</script>");
return;
}
if (RadUploadContext.Current.UploadedFiles[0].ContentLength >= 2147483647)
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('上传的文件不得超过 2GB !')</script>");
return;
}
UploadedFile file = RadUploadContext.Current.UploadedFiles[0];
string fileName = Path.GetFileName(file.FileName);
string virtualPath = System.IO.Path.Combine("~/save", fileName);
string savePath = this.MapPath(virtualPath);
file.SaveAs(savePath, true);
}3. 优点与缺点
优点:支持多线程操作,提供实时上传进度显示。
缺点:需要额外的学习和配置成本。
二、Bestcomy.Web.Controls.Upload
1. 简介
Bestcomy.Web.Controls.Upload是一款国人开发的ASP.NET上传控件,支持大文件上传,并提供多种配置选项。
2. 安装与配置
下载组件:从官网下载Bestcomy.Web.Controls.Upload组件。

添加到项目:将dll文件添加到项目的引用中。
Web.config配置:
<configuration>
<configSections>
<section name="aspnetUploadSettings" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.5000.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"/>
</configSections>
<aspnetUploadSettings>
<add key="maxRequestLength" value="2048000"/>
</aspnetUploadSettings>
<system.web>
<httpModules>
<add name="UploadModule" type="Bestcomy.Web.Controls.Upload.UploadModule,Bestcomy.Web.Controls.Upload"/>
</httpModules>
</system.web>
</configuration>前台代码:
<input type="file" name="file1" /> <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
String fpath = Path.Combine(Request.PhysicalApplicationPath, "UploadFiles");
if (!Directory.Exists(fpath))
{
Directory.CreateDirectory(fpath);
}
AspnetUpload upldr = new AspnetUpload();
upldr.set_UploadFolder(fpath);
upldr.RegisterModelessProgressBar(this.Button1);
}
protected void Button1_Click(object sender, EventArgs e)
{
String fpath = Path.Combine(Request.PhysicalApplicationPath, "UploadFiles");
UploadFileCollection files = AspnetUpload.GetUploadFiles("file1");
foreach (UploadFile file in files)
{
if (file != null)
{
file.SaveAs(Path.Combine(fpath, Path.GetFileName(file.get_FileName())));
}
}
}3. 优点与缺点
优点:配置简单,易于上手。
缺点:免费版本有文件大小限制。
三、Lion.Web.UpLoadModule
1. 简介
Lion.Web.UpLoadModule是一款基于HttpModule的大文件上传组件,支持分块读取数据,适合处理超大文件上传。
2. 安装与配置
下载组件:从官方网站下载Lion.Web.UpLoadModule组件。
添加到项目:将dll文件添加到项目的引用中。
Web.config配置:
<configuration>
<system.web>
<httpModules>
<add name="LionSky_UpLoadModule" type="LionSky.Web.UpLoadModule, LionSky.Web.UpLoadModule"/>
</httpModules>
</system.web>
</configuration>前台代码:
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" id="File1" name="File1" />
<asp:Button ID="UploadButton" runat="server" Text="上传" OnClick="UploadButton_Click" />
</form>后台代码:
protected void UploadButton_Click(object sender, EventArgs e)
{
HttpPostedFile postedFile = Request.Files["File1"];
if (postedFile != null && postedFile.ContentLength > 0)
{
string savePath = Server.MapPath("~/Uploads/") + Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(savePath);
}
}3. 优点与缺点
优点:支持分块读取,适合超大文件上传。
缺点:需要一定的配置和调试。
四、AspNetUpload
1. 简介
AspNetUpload是一款国人开发的收费ASP.NET上传控件,支持多种功能,包括大文件上传、多文件上传等。
2. 安装与配置
下载组件:从官网下载AspNetUpload组件。
添加到项目:将dll文件添加到项目的引用中。
Web.config配置:
<configuration>
<configSections>
<section name="aspnetUploadSettings" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.5000.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"/>
</configSections>
<aspnetUploadSettings>
<add key="maxRequestLength" value="2048000"/>
</aspnetUploadSettings>
<system.web>
<httpModules>
<add name="UploadModule" type="Bestcomy.Web.Controls.Upload.UploadModule,Bestcomy.Web.Controls.Upload"/>
</httpModules>
</system.web>
</configuration>前台代码:
<input type="file" name="file1" /> <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
String fpath = Path.Combine(Request.PhysicalApplicationPath, "UploadFiles");
if (!Directory.Exists(fpath))
{
Directory.CreateDirectory(fpath);
}
AspnetUpload upldr = new AspnetUpload();
upldr.set_UploadFolder(fpath);
upldr.RegisterModelessProgressBar(this.Button1);
}
protected void Button1_Click(object sender, EventArgs e)
{
String fpath = Path.Combine(Request.PhysicalApplicationPath, "UploadFiles");
UploadFileCollection files = AspnetUpload.GetUploadFiles("file1");
foreach (UploadFile file in files)
{
if (file != null)
{
file.SaveAs(Path.Combine(fpath, Path.GetFileName(file.get_FileName())));
}
}
}3. 优点与缺点
优点:功能全面,支持多种上传方式。
缺点:收费组件,成本较高。
| 组件名称 | 优点 | 缺点 |
| Telerik RadUpload for ASP.NET AJAX | 支持多线程操作,提供实时上传进度显示 | 需要额外的学习和配置成本 |
| Bestcomy.Web.Controls.Upload | 配置简单,易于上手 | 免费版本有文件大小限制 |
| Lion.Web.UpLoadModule | 支持分块读取,适合超大文件上传 | 需要一定的配置和调试 |
| AspNetUpload | 功能全面,支持多种上传方式 | 收费组件,成本较高 |
选择合适的上传组件应根据具体需求和预算来决定,如果需要免费的解决方案,可以考虑使用Bestcomy.Web.Controls.Upload或Lion.Web.UpLoadModule;如果预算允许,Telerik RadUpload for ASP.NET AJAX和AspNetUpload也是不错的选择。
小伙伴们,上文介绍了“asp上传大文件组件”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/3983.html<
