asp.net 上传大文件(100M) 看看思归大哥的http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用组件的比较多,自己写也行.ASP.NET 应用中大文件上传研究在项目中经常要用到了大文件上传,要上传的文件有100多m,于是研究现在国内使用的大文件上传的组件发现用的比较多的有两个控件AspnetUpload 2.0和Lion.Web.UpLoadModule,两个控件的方法是:利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody 和 ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。Chris Hynes为我们提供了这样的一个方案(用HttpModule),该方案除了允许你上传大文件外,还能实时显示上传进度。 Lion.Web.UpLoadModule和AspnetUpload 两个.NET组件都是利用的这个方案。*******这个所谓的方案可以在这里找到:*******http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx 当上传单文件时,两个软件的方法是一样的,继承HttpModuleHttpApplication application1 = sender as HttpApplication;HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));try{ if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1) { return; } //Check The HasEntityBody if (!request1.HasEntityBody()) { return; } int num1 = 0; TimeSpan span1 = DateTime.Now.Subtract(this.beginTime); string text1 = application1.Context.Request.ContentType.ToLower(); byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray()); int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11)); Progress progress1 = new Progress(); application1.Context.Items.Add("FileList", new Hashtable()); byte[] buffer2 = request1.GetPreloadedEntityBody(); num1 += buffer2.Length; string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID"); if (text2 != string.Empty) { application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2); } bool flag1 = true; if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3))) { flag1 = false; } if ((0 > span1.TotalHours) || (span1.TotalHours > 3)) { flag1 = false; } string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder"); ArrayList list1 = new ArrayList(); RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty); list1.AddRange(stream1.ReadBody); if (text2 != string.Empty) { progress1.FileLength = num2; progress1.ReceivedLength = num1; progress1.FileName = stream1.OriginalFileName; progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count; application1.Application["_UploadGUID_" + text2] = progress1; } if (!request1.IsEntireEntityBodyIsPreloaded()) { byte[] buffer4; ArrayList list2; int num3 = 204800; byte[] buffer3 = new byte[num3]; while ((num2 - num1) >= num3) { if (!application1.Context.Response.IsClientConnected) { this.ClearApplication(application1); } num3 = request1.ReadEntityBody(buffer3, buffer3.Length); num1 += num3; list2 = stream1.ContentBody; if (list2.Count > 0) { buffer4 = new byte[list2.Count + buffer3.Length]; list2.CopyTo(buffer4, 0); buffer3.CopyTo(buffer4, list2.Count); stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName); } else { stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName); } list1.AddRange(stream1.ReadBody); if (text2 != string.Empty) { progress1.ReceivedLength = num1; progress1.FileName = stream1.OriginalFileName; progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count; application1.Application["_UploadGUID_" + text2] = progress1; } } buffer3 = new byte[num2 - num1]; if (!application1.Context.Response.IsClientConnected && (stream1.FStatus == RequestStream.FileStatus.Open)) { this.ClearApplication(application1); } num3 = request1.ReadEntityBody(buffer3, buffer3.Length); list2 = stream1.ContentBody; if (list2.Count > 0) { buffer4 = new byte[list2.Count + buffer3.Length]; list2.CopyTo(buffer4, 0); buffer3.CopyTo(buffer4, list2.Count); stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName); } else { stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName); } list1.AddRange(stream1.ReadBody); if (text2 != string.Empty) { progress1.ReceivedLength = num1 + buffer3.Length; progress1.FileName = stream1.OriginalFileName; progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count; if (flag1) { progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded; } else { application1.Application.Remove("_UploadGUID_" + text2); } } } byte[] buffer5 = new byte[list1.Count]; list1.CopyTo(buffer5); this.PopulateRequestData(request1, buffer5);}catch (Exception exception1){ this.ClearApplication(application1); throw exception1;} 100MB ? asp.net 内置 http 上传不适合干这种事情, 得组建 用 <asp:FileUpload CssClass="Input" runat="server" ID="filecontrol1" />webconfig文件里,在SystemWeb下定义 <system.web> <httpRuntime maxRequestLength="2097150"></httpRuntime> </...> webconfig文件里配置一下 <system.web> <httpRuntime maxRequestLength="9999999"></httpRuntime> </...> kongwei521(蝶恋花雨) ※ ustbwuyi() 的方法 才是根本 否则 什么都是免谈! 如果是局域网,用自带的控件也行.<httpRuntime executionTimeout="500" maxRequestLength="204800" useFullyQualifiedRedirectUrl="false"/>如果是外网,最好是用 FTP上传, 如果网速不是很好的话,100M 够你喝N杯咖啡的时间了. 100M??也太大了吧这个只能在web.config设置<httpRuntime maxRequestLength="10240" executionTimeout="1024000"/> <httpRuntime maxRequestLength="1024000" executionTimeout="3600000"/>不好意思写错了 kongwei521(蝶恋花雨) ( ) 信誉:100 2007-8-7 20:41:33 得分: 0 webconfig文件里配置一下<system.web><httpRuntime maxRequestLength="9999999"></httpRuntime> </...>一看就知道没试过,可能设这么大么? 呵呵,前一阵子才弄了一个。我的可以上传1Gupload.9tdj.com.cn 需要在web。config文件里配置一下maxRequestLength 最大为4g(4096k)<system.web><httpRuntime maxRequestLength="4096000"></httpRuntime> </...>如果你使用了fileUpload也需要在里面修改一个值 那个的单位是byte 好像 上传100m以上的文件是没有问题的 http://www.lionsky.net/MyWebSite/DownSoft/List.Aspx?ID=225去这个地方下载一个,自己研究研究就行了,别忘记给我分啊 这个组件有很多,基本就是用HttpWorkerRequest类来实现,边传边写,不过各个组件效率也是差很多的看看这个:http://www.cnblogs.com/MaxIE/archive/2007/04/11/708255.aspx 有可以发[email protected] 也发我一份吧,谢谢![email protected] 普通的大文件大文件上传服务器的资源100%占用,除非用流传递或者控制服务器资源最大占用值 可以使用 NickLee.Common.SlickUpload<slickUpload> <uploadLocationProvider provider="File" location="~/upload" existingAction="Overwrite" fileNameMethod="Custom" customFileNameHandler="FortuneWeb.LetterFolderCustomFileNameGenerator, FortuneWeb"/> </slickUpload>参考下载地址:http://www.cnblogs.com/mail-ricklee下载最新的资源包,相信还有其他惊喜,里面的asp.net资源库 我把孟子E章的WebService上传大文件改成了类/// <summary> /// 上传文件.大柄修改 /// </summary> /// <param name="path">文件保存位置</param> /// <param name="oFile">文件</param> /// <returns></returns> public string UpLoadFile(string path, System.Web.HttpFileCollection oFile) { string rStr = ""; //首先得到上载文件信息和文件流 //System.Web.HttpFileCollection oFile = System.Web.HttpContext.Current.Request.Files; if (oFile.Count < 1) { return "请选择文件"; } string filePath = oFile[0].FileName; if (filePath == null || filePath == "") { return "请选择文件!"; } string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); try { ///处理上载的文件流信息 /// byte[] b = new byte[oFile[0].ContentLength]; Stream fs; //InLoadFiles oLoad = new InLoadFiles(); //UpLoad u = new UpLoad(); fs = (Stream)oFile[0].InputStream; fs.Read(b, 0, oFile[0].ContentLength); rStr = UpLoadStream(b, fileName, path); //调用Web Services的UploadFile方法进行上载文件。 //Response.Write(oLoad.UpLoadFile(b, fileName, ddlCategory.SelectedValue.ToString())); //Response.Write("<a href='javascript:history.go(-1)'>返回</a>"); //Response.End(); } catch (Exception error) { rStr = error.Message; //Response.Write(error.Message); //Response.End(); } return rStr; } /// <summary> /// 上传文件流.大柄修改 /// </summary> /// <param name="fs">流文件</param> /// <param name="fileName">文件名称</param> /// <param name="requestPath">保存路径</param> /// <returns></returns> protected string UpLoadStream(byte[] fs, string fileName, string requestPath) { try { string oldName = System.IO.Path.GetFileName(fileName); string expendName = System.IO.Path.GetExtension(oldName); string newName = DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "").Replace("/", ""); ///定义并实例化一个内存流,以存放提交上来的字节数组 /// MemoryStream m = new MemoryStream(fs); ///定义实际文件对象,保存上载的文件。 FileStream f = new FileStream(requestPath + newName + expendName, FileMode.Create); ///把内内存里的数据写入物理文件 m.WriteTo(f); m.Close(); f.Close(); f = null; m = null; return requestPath + newName + expendName; } catch (Exception error) { return error.Message; } }然后修改web.config<httpRuntime maxRequestLength="4096000"></httpRuntime>我上传200M也没有问题,不过就是消耗系统资源太多了 用NeatUpload这个控件,最近我做了个电影网站,用它上传电影都可以。 不要说附件大,没有几个能往正题上答的,人家头号就说明要传这么大的。我以前也问过同样的问题,也同样是没有结果,回答的都很离谱。我曾做过一个程序,要上传500M左右的视频,当时好像用的是Flash控件上传,楼主可以找找,搜索FlashUpload就行了,很好用,并且速度也很快! FlashUpload不错,刚测试了一下.不过cpu有瞬间达到100%的情况,不过基本还是不错地 给我发一个,谢谢![email protected] 在web.config中设置:<system.web><httpRuntime maxRequestLength="40960" //指定输入流缓冲阈值限制(单位为KB),即上传的最大限制,默认4MexecutionTimeout="3600" //允许执行请求的最大秒数, 只有当调试属性compilation= False时,此超时属性才适用appRequestQueueLimit="1000"/> //指定应用程序排队的请求的最大数目。当自由线程不够时,将对请求进行排队。当排队队列超出了设定值时,将显示“503 - 服务器太忙”错误信息拒绝传入的请求。 <compilation debug="true" defaultLanguage="c#"> </system.web> treeview点击后查询数据显示在gridview里 vs2008在调试模式下编辑后台代码 Asp.net 2.0 jQuery取数据失败 大文件上传控件NeatUpload,在VS2012里用不了是怎么回事? 再问HTML问题? 求一正则表达式 紧急问题有关iframe 问2个简单的问题(在线) 不在框架中,a界面上有一个datagrid,弹出的窗口b,如何关闭后b刷新datagrid的数据呢? 高分求助:项目开发流程问题 求救,Calendar选择时间范围的问题! asp.net 操作滚动图片新闻
http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx 当上传单文件时,两个软件的方法是一样的,继承HttpModuleHttpApplication application1 = sender as HttpApplication;
HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
try
{
if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1)
{
return;
}
//Check The HasEntityBody
if (!request1.HasEntityBody())
{
return;
}
int num1 = 0;
TimeSpan span1 = DateTime.Now.Subtract(this.beginTime); string text1 = application1.Context.Request.ContentType.ToLower(); byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray());
int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));
Progress progress1 = new Progress(); application1.Context.Items.Add("FileList", new Hashtable()); byte[] buffer2 = request1.GetPreloadedEntityBody();
num1 += buffer2.Length; string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");
if (text2 != string.Empty)
{
application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);
}
bool flag1 = true;
if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))
{
flag1 = false;
}
if ((0 > span1.TotalHours) || (span1.TotalHours > 3))
{
flag1 = false;
}
string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");
ArrayList list1 = new ArrayList();
RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);
list1.AddRange(stream1.ReadBody);
if (text2 != string.Empty)
{
progress1.FileLength = num2;
progress1.ReceivedLength = num1;
progress1.FileName = stream1.OriginalFileName;
progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
application1.Application["_UploadGUID_" + text2] = progress1;
}
if (!request1.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer4;
ArrayList list2;
int num3 = 204800;
byte[] buffer3 = new byte[num3];
while ((num2 - num1) >= num3)
{
if (!application1.Context.Response.IsClientConnected)
{
this.ClearApplication(application1);
}
num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
num1 += num3;
list2 = stream1.ContentBody;
if (list2.Count > 0)
{
buffer4 = new byte[list2.Count + buffer3.Length];
list2.CopyTo(buffer4, 0);
buffer3.CopyTo(buffer4, list2.Count);
stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
}
else
{
stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
}
list1.AddRange(stream1.ReadBody);
if (text2 != string.Empty)
{
progress1.ReceivedLength = num1;
progress1.FileName = stream1.OriginalFileName;
progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
application1.Application["_UploadGUID_" + text2] = progress1;
}
}
buffer3 = new byte[num2 - num1];
if (!application1.Context.Response.IsClientConnected && (stream1.FStatus == RequestStream.FileStatus.Open))
{
this.ClearApplication(application1);
}
num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
list2 = stream1.ContentBody;
if (list2.Count > 0)
{
buffer4 = new byte[list2.Count + buffer3.Length];
list2.CopyTo(buffer4, 0);
buffer3.CopyTo(buffer4, list2.Count);
stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
}
else
{
stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
}
list1.AddRange(stream1.ReadBody);
if (text2 != string.Empty)
{
progress1.ReceivedLength = num1 + buffer3.Length;
progress1.FileName = stream1.OriginalFileName;
progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
if (flag1)
{
progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;
}
else
{
application1.Application.Remove("_UploadGUID_" + text2);
}
}
}
byte[] buffer5 = new byte[list1.Count];
list1.CopyTo(buffer5);
this.PopulateRequestData(request1, buffer5);
}
catch (Exception exception1)
{
this.ClearApplication(application1);
throw exception1;
}
<asp:FileUpload CssClass="Input" runat="server" ID="filecontrol1" />webconfig文件里,在SystemWeb下
定义
<system.web>
<httpRuntime maxRequestLength="2097150"></httpRuntime>
</...>
<system.web>
<httpRuntime maxRequestLength="9999999"></httpRuntime>
</...>
executionTimeout="500"
maxRequestLength="204800"
useFullyQualifiedRedirectUrl="false"/>如果是外网,最好是用 FTP上传, 如果网速不是很好的话,100M 够你喝N杯咖啡的时间了.
也太大了吧
这个只能在web.config设置
<httpRuntime maxRequestLength="10240" executionTimeout="1024000"/>
webconfig文件里配置一下
<system.web>
<httpRuntime maxRequestLength="9999999"></httpRuntime>
</...>一看就知道没试过,可能设这么大么?
upload.9tdj.com.cn
<httpRuntime maxRequestLength="4096000"></httpRuntime>
</...>如果你使用了fileUpload也需要在里面修改一个值 那个的单位是byte 好像 上传100m以上的文件是没有问题的
去这个地方下载一个,自己研究研究就行了,别忘记给我分啊
看看这个:http://www.cnblogs.com/MaxIE/archive/2007/04/11/708255.aspx
[email protected]
[email protected]
可以使用 NickLee.Common.SlickUpload
<slickUpload>
<uploadLocationProvider provider="File" location="~/upload" existingAction="Overwrite" fileNameMethod="Custom" customFileNameHandler="FortuneWeb.LetterFolderCustomFileNameGenerator, FortuneWeb"/>
</slickUpload>参考下载地址:
http://www.cnblogs.com/mail-ricklee
下载最新的资源包,相信还有其他惊喜,里面的asp.net资源库
/// <summary>
/// 上传文件.大柄修改
/// </summary>
/// <param name="path">文件保存位置</param>
/// <param name="oFile">文件</param>
/// <returns></returns>
public string UpLoadFile(string path, System.Web.HttpFileCollection oFile)
{
string rStr = "";
//首先得到上载文件信息和文件流
//System.Web.HttpFileCollection oFile = System.Web.HttpContext.Current.Request.Files;
if (oFile.Count < 1)
{
return "请选择文件";
}
string filePath = oFile[0].FileName;
if (filePath == null || filePath == "")
{
return "请选择文件!";
}
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); try
{
///处理上载的文件流信息
///
byte[] b = new byte[oFile[0].ContentLength];
Stream fs;
//InLoadFiles oLoad = new InLoadFiles();
//UpLoad u = new UpLoad();
fs = (Stream)oFile[0].InputStream; fs.Read(b, 0, oFile[0].ContentLength); rStr = UpLoadStream(b, fileName, path); //调用Web Services的UploadFile方法进行上载文件。
//Response.Write(oLoad.UpLoadFile(b, fileName, ddlCategory.SelectedValue.ToString()));
//Response.Write("<a href='javascript:history.go(-1)'>返回</a>");
//Response.End();
}
catch (Exception error)
{
rStr = error.Message;
//Response.Write(error.Message);
//Response.End();
}
return rStr;
}
/// <summary>
/// 上传文件流.大柄修改
/// </summary>
/// <param name="fs">流文件</param>
/// <param name="fileName">文件名称</param>
/// <param name="requestPath">保存路径</param>
/// <returns></returns>
protected string UpLoadStream(byte[] fs, string fileName, string requestPath)
{
try
{
string oldName = System.IO.Path.GetFileName(fileName);
string expendName = System.IO.Path.GetExtension(oldName);
string newName = DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "").Replace("/", "");
///定义并实例化一个内存流,以存放提交上来的字节数组
///
MemoryStream m = new MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
FileStream f = new FileStream(requestPath + newName + expendName, FileMode.Create);
///把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return requestPath + newName + expendName;
}
catch (Exception error)
{
return error.Message;
}
}
然后修改web.config
<httpRuntime maxRequestLength="4096000"></httpRuntime>我上传200M也没有问题,不过就是消耗系统资源太多了
给我发一个,谢谢!
[email protected]
<httpRuntime
maxRequestLength="40960" //指定输入流缓冲阈值限制(单位为KB),即上传的最大限制,默认4M
executionTimeout="3600" //允许执行请求的最大秒数, 只有当调试属性compilation= False时,此超时属性才适用
appRequestQueueLimit="1000"/> //指定应用程序排队的请求的最大数目。当自由线程不够时,将对请求进行排队。当排队队列超出了设定值时,将显示“503 - 服务器太忙”错误信息拒绝传入的请求。
<compilation debug="true" defaultLanguage="c#">
</system.web>