![](/img/trans.png)
[英]Exception of type 'System.OutOfMemoryException' was thrown while uploading Blob
[英]'System.OutOfMemoryException' was thrown while uploading large file
当我上传大于500 MB的文件时,我收到以下错误
“抛出了'System.OutOfMemoryException'类型的异常。”
我的代码如下所示:
public readonly string Filename, ContentType;
public readonly int Size;
public readonly byte[] Bytes;
public FileHolder(HttpPostedFile file)
{
Filename = Path.GetFileName(file.FileName);
ContentType = GetContentType(file.ContentType, Filename);
Size = file.ContentLength;
Bytes = new byte[file.InputStream.Length]; //Here i get error
file.InputStream.Read(Bytes, 0, Size);
}
不要试图一次读取整个流。 无论如何,你不会同时获得整个流。
创建一个合理大小的缓冲区,然后一次读取一个块:
byte[] buffer = new byte[8192];
int offset = 0;
int left = Size;
while (left > 0) {
int len = file.InputStream.Read(buffer, 0, Math.Min(buffer.Length, left));
left -= len;
// here you should store the first "len" bytes of the buffer
offset += len;
}
您不应将整个文件加载到字节数组中。 相反,您可以直接从输入流处理文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.