ASP.NET对大文件上传的解决方案

所属分类: 网络编程 / ASP.NET 阅读数: 1025
收藏 0 赞 0 分享

首先,我们需要下载这个名为 RanUpLoad 的组件。

下载完成之后,两个 dll 文件添加到项目的引用中区,xml 文件也要复制在项目中的 bin 文件夹下,也就是最后三个文件都要存在于 bin 文件夹中。

接着,上传控件还是用 ASP.NET 中自带的 FileUpload 控件,需要添加的就是在 FileUpload 控件旁边加入标签:

<radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />
<radU:RadProgressArea ID="progressArea1" Width="100%" runat="server">
</radU:RadProgressArea>

并且在 aspx 文件的起始处添加如下代码:

<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" Assembly="Telerik.QuickStart" %>
<%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.Net2" %>

当然,配置文件的 <system.web> 标签中不能忘记下面这些语句:

<httpRuntime executionTimeout="3600" maxRequestLength="2097151" ></httpRuntime>
<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>

现在,外部的轮廓都已经布好了,接下来就是点击上传之后服务器端所需的操作:

当然,做这些操作之前,我们先 using 一下 Telerik.WebControls 命名空间。

// 检查文件
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); 

至此,文件上传的处理工作已经完成,以上的cs代码是我自己的一些操作处理,大家可以根据自己情况酌情修改,比如也可以放置多个FileUpload 控件,

用foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles){ ... }  这样的方式处理多个文件的上传。

希望此篇文章可以帮助对大文件上传头疼的朋友们去轻松处理上传问题。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多