我无法将InputStream
(containsint文件内容)作为byte
数组写入数据库。
我有以下代码,但是对于大文件,它会强制OutOfMemoryExceptoin
。
var postedFile = HttpContext.Current.Request.Files["file"];
byte[] fileContent;
using (MemoryStream ms = new MemoryStream())
{
postedFile.InputStream.CopyTo(ms); //error occures here
fileContent = ms.ToArray();
}
// updating entity with new filecontent using INSERT statement
您能否建议我如何将InputStream正确转换为字节数组,以便将其写入数据库?我们需要支持最多100mb(编辑:我已经从200更改为100)文件。
P.S。我知道这些是非常大的文件,但是现在这是处理它们的唯一方法。
答案 0 :(得分:0)
public byte[] FileToByteArray(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName,
FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int) numBytes);
return buff;
}
试试这个,让我知道它是否对你有用......按照我的说法,它应该有用......