【发布时间】:2014-01-10 07:10:33
【问题描述】:
当我上传此文件时,我的文件名类似于“c3c81d9e-b390-4622-999c-eb74146afd10.dat”。 我不想更改上传逻辑。此文件大小为 1 GB。
我的问题是,当不同的用户同时下载同一个文件时,它会抛出异常 - 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尝试将所有文件读入内存,所以不要将它用于大文件,使用与流一起使用的东西 -
什么是
file变量?你的意思是f? -
你应该使用一些缓存。这不仅可以消除您的记忆问题,而且速度也会快很多。
-
我想知道你是否理解第二个代码被截断的作用。方法
ComputeHash计算散列(通过从流中逐字节或逐块读取),没有数据,foreach所做的是将散列值(字节)转换为字符串。而不是File.ReadAllBytes,您必须使用FileStream.Read来读取部分内容,您可以将其保存在内存中。 -
你好,看看我的解决方案,它会对你有所帮助....
标签: c#