C# 上传大文件(1GB)-ASP.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1599409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload Large files(1GB)-ASP.net
提问by
I need to upload large files of at least 1GB
file size.
I am using ASP.Net
, C#
and IIS 5.1
as my development platform.
我需要上传至少1GB
文件大小的大文件。我使用ASP.Net
,C#
并IIS 5.1
作为我的开发平台。
I am using:
我在用:
HIF.PostedFile.InputStream.Read(fileBytes,0,HIF.PostedFile.ContentLength)
before using:
使用前:
File.WriteAllBytes(filePath, fileByteArray)
(doesnt go here but gives System.OutOfMemoryException
exception)
(不去这里但给出System.OutOfMemoryException
例外)
Currently I have set the httpRuntime
to:
目前我已经设置httpRuntime
为:
executionTimeout="999999" maxRequestLength="2097151"(thats 2GB!) useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="5000" enableVersionHeader="true" requestLengthDiskThreshold="8192"
executionTimeout=" 999999" maxRequestLength=" 2097151"(那是 2GB!) useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="5000" enableVersionHeader="true" requestLengthDiskThreshold="8192"
Also i have set maxAllowedContentLength="**2097151**"
(guess its only for IIS7)
我也设置了maxAllowedContentLength="**2097151**"
(猜它只适用于 IIS7)
I have changed IIS
connection timeout to 999,999 secs too.
我也将IIS
连接超时更改为 999,999 秒。
I am unable to upload files of even 4578KB
(Ajaz-Uploader.zip)
我无法上传甚至4578KB
(Ajaz-Uploader.zip) 的文件
回答by Manitra Andriamitondra
Try copying without loading every thing in the memory :
尝试复制而不加载内存中的所有内容:
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;
}
回答by Audrius
Check this blog entryabout large file uploads. It also has a few links to some discussion forums that can shed some light on this as well. The suggestion is to use custom HttpHandler for that or custom Flash/Silverlight control.
检查这个关于大文件上传的博客条目。它还有一些指向一些论坛的链接,这些链接也可以对此有所了解。建议使用自定义 HttpHandler 或自定义 Flash/Silverlight 控件。
回答by Manish Sinha
I googled and found - NeatUpload
我用谷歌搜索并发现 - NeatUpload
Another solution would be to read the bytes on the client and send it to the server, the server saves the file. Example
另一种解决方案是读取客户端上的字节并将其发送到服务器,服务器保存文件。例子
Server: in Namespace - Uploader, class - Upload
服务器:在命名空间 - 上传者,类 - 上传
[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;
}
Client:
客户:
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);
回答by adrianos
For IIS 6.0 you can change AspMaxEntityAllowed in Metabase.xml, but I don't think it's as straight forward in IIS 5.1.
对于 IIS 6.0,您可以更改 Metabase.xml 中的 AspMaxEntityAllowed,但我认为它在 IIS 5.1 中没有那么简单。
This link may help, hope it does:
此链接可能会有所帮助,希望它可以:
回答by Arturo Caballero
I think you should use Response.TransmitFile, this method does not load in web server memory the file, it streams the file without using web server resources.
我认为您应该使用 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();
}
回答by Shawn South
We have an app that occasionally needs to upload 1 and 2 GB files, so has been running into this as well. After much research, my conclusion is that we need to implement the previously mentioned NeatUpload, or something like it.
我们有一个应用程序偶尔需要上传 1 GB 和 2 GB 的文件,所以也遇到了这个问题。经过大量研究,我的结论是我们需要实现前面提到的NeatUpload或类似的东西。
Also, be aware that
另外,请注意
<requestLimits maxAllowedContentLength=.../>
is measured in bytes, while
以字节为单位,而
<httpRuntime maxRequestLength=.../>
is measured in kilobytes. So your values should look more like this:
以千字节为单位。所以你的价值观应该更像这样:
<httpRuntime maxRequestLength="2097151"/>
...
<requestLimits maxAllowedContentLength="2097151000"/>
回答by Genady Sergeev
Setting maxRequestLength should be enough for uploading files larger than 4mb, which is the default limit for HTTP request size. Please make extra sure that nothing is overriding your config file.
设置 maxRequestLength 应该足以上传大于 4mb 的文件,这是 HTTP 请求大小的默认限制。请额外确保没有任何内容覆盖您的配置文件。
Alternatively you can check the async upload provided by Telerik, which uploads files by 2mb chunks and effectively can bypass the ASP.NET request size limitation.
或者,您可以检查Telerik 提供的异步上传,它以 2mb 块上传文件,并且可以有效地绕过 ASP.NET 请求大小限制。
回答by damir
I know it is an old question, but still unanswered.
我知道这是一个老问题,但仍然没有答案。
So this is what you have to do:
所以这是你必须做的:
In you web.config file, add this in :
在您的 web.config 文件中,将其添加到:
<!-- 3GB Files / in kilobyte (3072*1024) -->
<httpRuntime targetFramework="4.5" maxRequestLength="3145728"/>
and this under
而这下
<security>
<requestFiltering>
<!-- 3GB Files / in byte (3072*1024*1024) -->
<requestLimits maxAllowedContentLength="3221225472" />
</requestFiltering>
</security>
You see in the comment how this works. In one you need to have the sie in bytes and in the other one in kilobytes. Hope that helps.
你在评论中看到这是如何工作的。在一个中,您需要以字节为单位,而在另一个中以千字节为单位。希望有帮助。