【发布时间】:2020-07-23 11:13:06
【问题描述】:
我有一个接受两个 HttpPostedFileBase 参数的函数。 函数一开始,我就转换为字节数组(如下所示)。
在转换为字节数组之前,我可以断点函数并查看ContentLength 属性有一个值(类似于 1123512),但有时其中一个字节数组设置为 0,当我再次检查 HttpPostedFileBase , ContentLength 设置为 0?
public static async Task FileCheck(HttpPostedFileBase Image1, HttpPostedFileBase Image2)
{
byte[] ByteArray1 = HttpPostedFileBaseToBase64ByteArray(Image1);
byte[] ByteArray2 = HttpPostedFileBaseToBase64ByteArray(Image2);
}
这里是字节数组转换:
public static byte[] HttpPostedFileBaseToBase64ByteArray(HttpPostedFileBase Image)
{
//Get bytes from image
byte[] ImageAsBytes = new byte[Image.ContentLength];
//Read image binary
using (BinaryReader BinaryReader = new BinaryReader(Image.InputStream))
{
//Insert into byte array
ImageAsBytes = BinaryReader.ReadBytes(Image.ContentLength);
}
return ImageAsBytes;
}
就像我说的,这只是偶尔发生,这很奇怪。
【问题讨论】:
-
在再次阅读之前尝试
Image.InputStream.Position = 0;。
标签: c# asp.net httppostedfilebase