Follow the code below:
Controller (with post action):
var file = Request.Files[0];
var bytes = ConvertTo.Bytes(file);
ConvertTo Class:
public static byte[] Bytes(HttpPostedFileBase result)
{
var length = result.InputStream.Length; //Length: 103050706
MemoryStream target = new MemoryStream();
result.InputStream.CopyTo(target); // gera problema nessa linha aqui
byte[] data = target.ToArray();
return data;
}
The file size is: 98.2 MB (103,050,706 bytes), file size 60 MB works perfectly.
In line: result.InputStream.CopyTo(target);
I get error:
System.OutOfMemoryException: 'Exception_WasThrown'
Small file works fine, only big file does this problem.
Any solution for large file?
UPDATE:
Follow the code below:
public static byte[] ConverToBytes(HttpPostedFileBase file)
{
var length = file.InputStream.Length; //Length: 103050706
byte[] fileData = null;
using (var binaryReader = new BinaryReader(file.InputStream))
{
fileData = binaryReader.ReadBytes(file.ContentLength);
}
return fileData;
}
Code above works using BinaryReader
. Why does MemoryStream
not work?