首页 文章

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

提问于
浏览
6

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

8 回答

  • 0

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

    另外,请注意

    <requestLimits maxAllowedContentLength=.../>
    

    bytes 中测量,而

    <httpRuntime maxRequestLength=.../>
    

    kilobytes 中测量 . 所以你的 Value 看起来应该更像这样:

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

    我用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);
    
  • 5

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

    所以这就是你要做的:

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

    你在评论中看到它是如何工作的 . 在一个中你需要以字节为单位,而另一个以千字节为单位 . 希望有所帮助 .

  • 0

    检查this blog entry有关大文件上传的信息 . 它还与一些讨论论坛有一些链接,可以对此有所了解 . 建议是使用自定义HttpHandler或自定义Flash / Silverlight控件 .

  • 0

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

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

    对于IIS 6.0,您可以在Metabase.xml中更改AspMaxEntityAllowed,但我不认为它在IIS 5.1中是直截了当的 .

    这个链接可能有所帮助,希望它能做到:

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

  • 0

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

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

  • 3

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

相关问题