【问题标题】:Upload Large files(1GB)-ASP.net上传大文件(1GB)-ASP.net
【发布时间】:2010-12-08 15:16:34
【问题描述】:

我需要上传至少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) 的文件

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    我用谷歌搜索发现 - 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);
    

    【讨论】:

    • 我没有检查代码。不确定这是否有效,只是想传达这个想法。这几乎是 FTP 所做的,除了端口 20/21 之外,一切都发生在端口 80 上。
    • 哇!我很高兴你提到了 NeatUpload。我为自己拿了一份副本,它使上传对我来说很容易添加到我的项目中。谢谢。
    • 我在 br.ReadBytes() 处出现内存不足异常。文件大小 ~700MB
    【解决方案2】:

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

    另外,请注意

    <requestLimits maxAllowedContentLength=.../>
    

    字节为单位,而

    <httpRuntime maxRequestLength=.../>
    

    千字节 为单位。所以你的价值观应该更像这样:

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

    【讨论】:

      【解决方案3】:

      我知道这是一个老问题,但仍然没有答案。

      所以这就是你必须做的:

      在您的 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>
      

      您在评论中看到这是如何工作的。在一个中,您需要以字节为单位,在另一个中以千字节为单位。希望有帮助。

      【讨论】:

      • 但这不适用于我的情况。你能详细说明如何使它工作吗? TIA
      【解决方案4】:

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

      或者,您可以检查async upload provided by Telerik,它以 2mb 块上传文件,并且可以有效地绕过 ASP.NET 请求大小限制。

      【讨论】:

        【解决方案5】:

        对于 IIS 6.0,您可以在 Metabase.xml 中更改 AspMaxEntityAllowed,但我认为这在 IIS 5.1 中并不那么简单。

        此链接可能会有所帮助,希望对您有所帮助:

        http://itonlinesolutions.com/phpbb3/viewtopic.php?f=3&t=63

        【讨论】:

          【解决方案6】:

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

          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在配置文件中。---> System.Web.HttpException: Maximum request length exceeded....." 异常。
          【解决方案7】:

          检查this blog entry 是否有大文件上传。它还有一些指向一些讨论论坛的链接,这些链接也可以对此有所了解。建议使用自定义 HttpHandler 或自定义 Flash/Silverlight 控件。

          【讨论】:

            【解决方案8】:

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

            【讨论】:

            • 不要打开内存中的文件,你会关闭服​​务器!!恕我直言,发布的所有其他解决方案似乎都适用于小文件,但不适用于 1Gig 文件
            • 这个方法可以反向使用,下载或者上传大文件,我看看能不能找到文档
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-19
            • 1970-01-01
            • 2016-03-23
            相关资源
            最近更新 更多