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

b91juud3  于 2023-06-07  发布在  .NET
关注(0)|答案(8)|浏览(382)

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

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

使用前:

File.WriteAllBytes(filePath, fileByteArray)

(此处不显示,但给出System.OutOfMemoryException例外)
现在我把httpRuntime设置为:
executionTimeout="99999”maxRequestLength="2097151"(即2GB!)useFullyQualifiedRedirectUrl=“true”minFreeThreads=“8”minLocalRequestFreeThreads=“4”appRequestQueueLimit=“5000”enableVersionHeader=“true”requestLengthDiskThreshold=“8192”
我还设置了maxAllowedContentLength="**2097151**"(猜它只适用于IIS 7)
我已经将IIS连接超时更改为999,999秒。
我甚至无法上传4578KB的文件(Ajaz-Uploader.zip)

nkhmeac6

nkhmeac61#

我们有一个应用程序,偶尔需要上传1和2 GB的文件,所以一直运行到这个以及。经过大量研究,我的结论是我们需要实现前面提到的NeatUpload,或者类似的东西。
另外,请注意

<requestLimits maxAllowedContentLength=.../>

字节为单位,而

<httpRuntime maxRequestLength=.../>

千字节为单位。因此,您的值应该更像这样:

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

e0bqpujr2#

我知道这是一个古老的问题,但仍然没有答案。
所以这就是你要做的:
在你的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>

你可以在评论中看到这是如何工作的。在一个你需要有在字节和在另一个千字节的sie。希望能帮上忙。

20jt8wwn

20jt8wwn3#

我在谷歌上搜索了一下,找到了-NeatUpload
另一种解决方案是读取客户端上的字节并将其发送到服务器,服务器保存文件。示例
服务器:在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;
}

客户:

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

dz6r00yl4#

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

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

wnrlj8wa5#

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

prdp8dxp6#

检查this blog entry关于大文件上传。它也有一些链接到一些讨论论坛,可以揭示一些关于这一点以及。建议为该控件或自定义Flash/Silverlight控件使用自定义HttpHandler。

qhhrdooz

qhhrdooz7#

对于IIS 6.0,您可以在Metabase.xml中更改AspMaxEntityAllowed,但我不认为它在IIS 5.1中是直接的。
这个链接可能会有帮助,希望它能:
http://itonlinesolutions.com/phpbb3/viewtopic.php?f=3&t=63

6ju8rftf

6ju8rftf8#

设置maxRequestLength应该足以上传大于4mb的文件,这是HTTP请求大小的默认限制。请特别确保没有任何东西覆盖您的配置文件。
或者,您可以检查async upload provided by Telerik,它以2MB的块上传文件,并且可以有效地绕过ASP.NET请求大小限制。

相关问题