上传大文件(1GB)-ASP.net

7 .net c#

我需要上传至少1GB文件大小的大文件.我正在使用ASP.Net,C#IIS 5.1作为我的开发平台.

我在用:

HIF.PostedFile.InputStream.Read(fileBytes,0,HIF.PostedFile.ContentLength)
Run Code Online (Sandbox Code Playgroud)

使用前:

File.WriteAllBytes(filePath, fileByteArray)
Run Code Online (Sandbox Code Playgroud)

(不会去这里,但给出System.OutOfMemoryException例外)

目前我已经设置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)

Sha*_*uth 7

我们有一个偶尔需要上传1和2 GB文件的应用程序,所以也遇到了这个问题.经过大量研究,我的结论是我们需要实现前面提到的NeatUpload,或类似的东西.

另外,请注意

<requestLimits maxAllowedContentLength=.../>
Run Code Online (Sandbox Code Playgroud)

中测量字节,而

<httpRuntime maxRequestLength=.../>
Run Code Online (Sandbox Code Playgroud)

千字节为单位.所以你的价值看起来应该更像这样:

<httpRuntime maxRequestLength="2097151"/>
...
<requestLimits maxAllowedContentLength="2097151000"/>
Run Code Online (Sandbox Code Playgroud)


Man*_*dra 1

尝试复制而不加载内存中的所有内容:

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;
}
Run Code Online (Sandbox Code Playgroud)