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

9

我需要上传至少 1GB 大小的大文件。

我的开发平台是 ASP.NetC#IIS 5.1

我正在使用:

HIF.PostedFile.InputStream.Read(fileBytes,0,HIF.PostedFile.ContentLength)

使用前:

File.WriteAllBytes(filePath, fileByteArray)

(这里不是问题,但会引发 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)的文件都无法上传。


这个回答解决了你的问题吗?ASP.NET中无法上传大文件 - Rajib Chy
你使用的是哪个 .NET 版本? - jmvcollaborator
8个回答

10

我们有一个应用程序偶尔需要上传1到2 GB的文件,因此也遇到了这个问题。经过大量研究,我的结论是我们需要实现之前提到的NeatUpload或类似的解决方案。

另外,请注意:

<requestLimits maxAllowedContentLength=.../>

计算机存储容量用字节来衡量,而

<httpRuntime maxRequestLength=.../>

文件大小以千字节为单位进行测量。因此,您的值应该像这样:

<httpRuntime maxRequestLength="2097151"/>
...
<requestLimits maxAllowedContentLength="2097151000"/>

4
我搜索并找到了 - NeatUpload


另一个解决方案是在客户端读取字节并将其发送到服务器,服务器保存文件。 例如:

服务器:在命名空间 - Uploader,类 - 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;
}

客户端:

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);

1
我还没有检查过代码。不确定这是否有效,只是想传达这个想法。这几乎就是FTP所做的事情,只是一切都在端口80上进行,而不是20/21端口。 - Manish Sinha
哇!我很高兴你提到了NeatUpload。我为自己拿了一份,它让上传变得非常容易,方便我添加到我的项目中。谢谢。 - Magnus Smith
我在 br.ReadBytes() 处遇到了内存不足的异常。文件大小约为 700MB。 - Görkem Hacıoğlu

4

我知道这是一个老问题,但仍未解答。

所以你需要做的是:

在你的web.config文件中添加以下内容:

    <!-- 3GB Files / in kilobyte (3072*1024) -->
    <httpRuntime targetFramework="4.5" maxRequestLength="3145728"/>

并且这是下方的内容

<security>
    <requestFiltering>

      <!-- 3GB Files / in byte (3072*1024*1024) -->
      <requestLimits maxAllowedContentLength="3221225472" />

    </requestFiltering>
</security>

您可以从注释中了解它的工作原理。在其中一个中,您需要以字节为单位,在另一个中则需要以千字节为单位。希望这能帮到您。


但是在我的情况下这并不起作用。您能否详细说明如何使其工作?谢谢。 - Raja Sekhar

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;
}

你好,manitra, 我尝试在客户端使用你的CopyFile()函数,在服务器端使用Copy()函数,但是我遇到了一些错误。 我还观察到一个问题,WriteAllBytes只能处理3MB以下的数据,超过这个大小就会出现"System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: There was an exception running the extensions specified in the config file. ---> System.Web.HttpException: Maximum request length exceeded....."异常。 - user193647

0

我认为你应该使用 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();
        }

请勿在内存中打开文件,否则会导致服务器崩溃!!恕我直言,其他发布的解决方案似乎只适用于小文件,而不适用于1G大文件。 - Arturo Caballero
这个方法可以反向使用,用于下载或上传大文件,让我看看能否找到文档。 - Arturo Caballero

0

0

设置maxRequestLength应该足以上传大于4mb的文件,这是HTTP请求大小的默认限制。请确保没有任何东西覆盖您的配置文件。

或者,您可以检查Telerik提供的异步上传,它通过2mb块上传文件,有效地可以绕过ASP.NET请求大小限制。


0

请查看此博客文章,了解有关大文件上传的信息。该文章还提供了一些讨论论坛的链接,可以帮助您更好地理解这个问题。建议使用自定义HttpHandler或自定义Flash/Silverlight控件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接