上传大文件(1GB)-ASP.net
我需要上传至少1GB
文件大小的大文件。 我使用ASP.Net
, C#
和IIS 5.1
作为我的开发平台。
我在用:
HIF.PostedFile.InputStream.Read(fileBytes,0,HIF.PostedFile.ContentLength)
使用前:
File.WriteAllBytes(filePath, fileByteArray)
(不会去这里,但会给出System.OutOfMemoryException
exception)
目前我已将httpRuntime
设置为:
executionTimeout =“ 999999 ”maxRequestLength =“ 2097151 ”(多数2GB!)useFullyQualifiedRedirectUrl =“true”minFreeThreads =“8”minLocalRequestFreeThreads =“4”appRequestQueueLimit =“5000”enableVersionHeader =“true”requestLengthDiskThreshold =“8192”
我也设置了maxAllowedContentLength="**2097151**"
(猜测它仅适用于IIS7)
我已将IIS
连接超时更改为999,999秒。
我无法上传甚至4578KB
文件(Ajaz-Uploader.zip)
我们有一个偶尔需要上传1和2 GB文件的应用程序,所以也遇到了这个问题。 经过大量研究,我的结论是我们需要实现前面提到的NeatUpload ,或类似的东西。
另外,请注意
是以字节为单位测量的
以千字节为单位。 所以你的价值看起来应该更像这样:
...
我用Google搜索并发现 – NeatUpload
另一种解决方案是读取客户端上的字节并将其发送到服务器,服务器保存文件。 例
服务器:在命名空间 – 上传器,类 – 上传
[WebMethod] public bool Write(String fileName, Byte[] data) { FileStream fs = File.Open(fileName, FileMode.Open); BinaryWriter bw = new BinaryWriter(fs); bw.Write(data); bw.Close(); return true; }
客户:
string filename = "C:\..\file.abc"; Uploader.Upload up = new Uploader.Upload(); FileStream fs = File.Create(fileName); BinaryReader br = new BinaryReader(fs); // Read all the bytes Byte[] data = br.ReadBytes(); up.Write(filename,data);
我知道这是一个老问题,但仍然没有答案。
所以这就是你要做的:
在您的web.config文件中,将其添加到:
而这下
你在评论中看到它是如何工作的。 在一个中你需要以字节为单位,而另一个以千字节为单位。 希望有所帮助。
查看此博客条目有关大文件上传的信息。 它还与一些讨论论坛有一些链接,这些论坛也可以对此有所了解。 建议是使用自定义HttpHandler或自定义Flash / Silverlight控件。
尝试复制而不加载内存中的所有内容:
public void CopyFile() { Stream source = HIF.PostedFile.InputStream; //your source file Stream destination = File.OpenWrite(filePath); //your destination Copy(source, destination); } public static long Copy(Stream from, Stream to) { long copiedByteCount = 0; byte[] buffer = new byte[2 << 16]; for (int len; (len = from.Read(buffer, 0, buffer.Length)) > 0; ) { to.Write(buffer, 0, len); copiedByteCount += len; } to.Flush(); return copiedByteCount; }
对于IIS 6.0,您可以在Metabase.xml中更改AspMaxEntityAllowed,但我不认为它在IIS 5.1中是直截了当的。
这个链接可能有所帮助,希望它能做到:
设置maxRequestLength应该足以上传大于4mb的文件,这是HTTP请求大小的默认限制。 请确保没有任何内容覆盖您的配置文件。
或者,您可以检查Telerik提供的异步上载 ,它通过2mb块上传文件,并且可以有效地绕过ASP.NET请求大小限制。
我认为你应该使用Response.TransmitFile,这种方法不会在web服务器内存中加载文件,它会在不使用Web服务器资源的情况下流式传输文件。
if (Controller.ValidateFileExist()) { ClearFields(); Response.Clear(); Response.ContentType = "text/plain"; Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "FileNAme.Ext")); Response.TransmitFile(FileNAme.Ext); Response.End(); Controller.DeleteFile(); }