如何从 HttpPostedFile 创建字节数组
问题描述:
我正在使用具有 FromBinary 方法的图像组件.想知道如何将输入流转换为字节数组
I'm using an image component that has a FromBinary method. Wondering how do I convert my input stream into a byte array
HttpPostedFile file = context.Request.Files[0];
byte[] buffer = new byte[file.ContentLength];
file.InputStream.Read(buffer, 0, file.ContentLength);
ImageElement image = ImageElement.FromBinary(byteArray);
答
使用 BinaryReader 对象从流中返回一个字节数组,如:
Use a BinaryReader object to return a byte array from the stream like:
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}