當我上傳這個文件時,我的文件名就像是「c3c81d9e-b390-4622-999c-eb74146afd10.dat」。 我不想更改上傳邏輯。該文件大小爲1 GB。正在讀取大文件 - System.OutOfMemoryException:拋出了類型爲「System.OutOfMemoryException」的異常。在C#
我的問題是,當不同的用戶下載同一個文件在同一時間,它拋出異常 - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown
。
我的代碼如下(C#)
string path ="E:\Projects\testsite\Inetpub\wwwroot\Downloads";
byte[] FileContents;
FileContents = File.ReadAllBytes(path);
但它給的System.OutOfMemoryException:類型 'System.OutOfMemoryException的' 引發的異常。
我使用堆棧溢出的以下解決方案。
Reading large file using byte[] gives error
byte[] hashValue;
SHA1Managed hashString = new SHA1Managed();
using (FileStream f = File.Open(path, FileMode.Open))
{
hashValue = hashString.ComputeHash(f);
foreach (byte x in hashValue)
{
file.HexadecimalValue += String.Format("{0:x2}", x);
}
}
但這file.HexadecimalValue
與哈希值返回的字符串。 如何從file.HexadecimalValue
中提取數據並將其傳遞給HttpContext.Current.Response
? 什麼是最好的解決方案?
在此先感謝
'File.ReadAllBytes'試圖讀取所有文件到內存中,所以不能將其用於大文件,使用與流 – Grundy
是什麼'file'變量來的東西嗎?你的意思是'f'? – Grundy
你應該使用一些緩存。這不僅可以消除你的記憶問題,而且速度也會快很多。 –