簡體   English   中英

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

[英]Upload Large files(1GB)-ASP.net

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

我們有一個應用程序偶爾需要上傳 1 GB 和 2 GB 的文件,所以也遇到了這個問題。 經過大量研究,我的結論是我們需要實現前面提到的NeatUpload或類似的東西。

另外,請注意

<requestLimits maxAllowedContentLength=.../>

字節為單位,而

<httpRuntime maxRequestLength=.../>

千字節為單位。 所以你的價值觀應該更像這樣:

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

我用谷歌搜索並發現 - 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);

我知道這是一個老問題,但仍然沒有答案。

所以這是你必須做的:

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

你在評論中看到這是如何工作的。 在一個中,您需要以字節為單位,而在另一個中以千字節為單位。 希望有幫助。

嘗試復制而不加載內存中的所有內容:

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

檢查這個關於大文件上傳的博客條目 它還有一些指向一些論壇的鏈接,這些鏈接也可以對此有所了解。 建議使用自定義 HttpHandler 或自定義 Flash/Silverlight 控件。

設置 maxRequestLength 應該足以上傳大於 4mb 的文件,這是 HTTP 請求大小的默認限制。 請額外確保沒有任何內容覆蓋您的配置文件。

或者,您可以檢查Telerik 提供異步上傳,它以 2mb 塊上傳文件,並且可以有效地繞過 ASP.NET 請求大小限制。

對於 IIS 6.0,您可以更改 Metabase.xml 中的 AspMaxEntityAllowed,但我認為它在 IIS 5.1 中沒有那么簡單。

此鏈接可能會有所幫助,希望它可以:

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

我認為您應該使用 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();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM