由于项目需要开发一个在线视频系统,功能需要跟优酷类似,但必须支持大文件的上传,由于原来系统是基于.net平台开发, 一般10M以下的文件上传通过设置Web.Config里面的 maxRequestLength属性即可,再用VS2008自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了。Google之,发现大体上有两种解决方案:
1.利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody 和 ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据
IServiceProvider provider = (IServiceProvider) HttpContext.Current;
HttpWorkerRequest wr = (HttpWorkerRequest) provider.GetService(typeof(HttpWorkerRequest));
byte[] bs = wr.GetPreloadedEntityBody();
....
if (!wr.IsEntireEntityBodyIsPreloaded())
{
int n = 1024;
byte[] bs2 = new byte[n];
while (wr.ReadEntityBody(bs2,n) >0)
{
.....
}
}
感觉该方法实现起来比较麻烦,况且没有较多的时间进行研究,有兴趣的朋友可以参考一下这篇文章:http://blog.joycode.com/saucer/archive/2004/03/16/16225.joy
2.采用第三方控件实现上传。这时找到了NeatUpload http://www.brettle.com/neatupload,看了一下Demo发现效果很不错,并且自带进度条,符合一般人的口味。因此决定选用该方案。下面具体介绍一下如何使用neatupload进行开发。
dotnet
flash
js
release
COPYING-LIB.txt
LICENSE.txt
NeatUpload.sln
ReadMe.txt
首先我们需要做的是将NeatUpload的两个主要控件:InputFile和progressbar 添加进VS2008。打开VS2008--->点击'工具箱'--->选中一项,如:'常规'这项--->单击右键--->在弹出的菜单中选'选择项'--->在弹出的窗口中选择'浏览'--->找到'Brettle.Web.NeatUpload.dll'这个文件 (在/dotnet/docs/bin文件目录下)--->按确定就添加上。
1 <configuration>
2
3 <configSections>
4
5 <sectionGroup name="system.web">
6
7 <section name="neatUpload"
8 type="Brettle.Web.NeatUpload.ConfigSectionHandler,Brettle.Web.NeatUpload" allowLocation="true" />
9
10 </sectionGroup>
11
12 </configSections>
13
14 <system.web>
15
16 <httpModules>
17
18 <!--上传大文件-->
19
20 <add name="UploadHttpModule"
21 type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload"/>
22
23 </httpModules>
24
25 <!--useHttpModule="true" 设为true时才能看到上传状态,默认是False-->
26
27 <neatUpload useHttpModule="true" maxNormalRequestLength="1048576" maxRequestLength="1048576" defaultProvider="FilesystemUploadStorageProvider">
28
29 <providers>
30
31 <add name="FilesystemUploadStorageProvider"
32 type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload"/>
33
34 </providers>
35
36 </neatUpload>
37
38 <httpRuntime maxRequestLength="1048576" executionTimeout="3600"/>
39
40 </system.web>
41
42 </configuration>
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinkedContent.aspx.cs"
2 Inherits="Koodoo.Web.LinkedContent" %>
3
4 <%@ Register assembly="Brettle.Web.NeatUpload" namespace="Brettle.Web.NeatUpload" tagprefix="Upload" %>
5
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7 <html xmlns="http://www.w3.org/1999/xhtml">
8 <head runat="server">
9 <title></title>
10 </head>
11 <body id="linkedcontent">
12 <form id="form1" runat="server">
13 <div class="pagebox">
14 <div class="innerbox">
15 <Upload:InputFile ID="attachFile" runat="server" />
16 <asp:Button ID="btnUpload" Text="上传" runat="server" onclick="btnUpload_Click" />
17 <br />
18 <Upload:ProgressBar ID="ProgressBar1" runat="server" />
19 </div>
20 </div>
21 </form>
22 </body>
23 </html>
给上传按钮添加Upload_Click事件: protected void Upload_Click(object sender, EventArgs e)
2 {
3 if (AttachFile.HasFile)
4 {
5 string FileName = this.AttachFile.FileName;//获取上传文件的文件名,包括后缀
6 string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
7 string SaveFileName = System.IO.Path.Combine(
8 System.Web.HttpContext.Current.Request.MapPath("UpLoads/"),
9 DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName);//合并两个路径为上传到服务器上的全路径
10 AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
11 string url = "UpLoads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName; //文件保存的路径
12 float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); //获取文件大小并保留小数点后一位,单位是M
13 }
14 }
这样就完成了一个基本的大文件上传模块。
参考文献如下:
http://www.cnblogs.com/hiearth/archive/2009/07/02/1515467.html
http://www.brettle.com/neatupload
http://www.brettle.com/Demo.aspx
http://blog.joycode.com/saucer/archive/2004/03/16/16225.joy