欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-18 12:35:16
首先,我们需要下载这个名为 ranupload 的组件。 下载完成之后,两个 dll 文件添加到项目的引用中区,xml 文件也要复制在项目中的 bin 文件夹下,也就是最...

首先,我们需要下载这个名为 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){ ... }  这样的方式处理多个文件的上传。

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