C# microsoft.aspnet.http.abstraction中缺少InputStream和ContentLength
正如我对Microsoft.AspNet.Http.Abstracts的回顾,该文件在ASP.NET MVC 6中缺少InputStream和ContentLength,它取代了OpenReadStream,后者没有响应转换字节的功能,例如:C# microsoft.aspnet.http.abstraction中缺少InputStream和ContentLength,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,正如我对Microsoft.AspNet.Http.Abstracts的回顾,该文件在ASP.NET MVC 6中缺少InputStream和ContentLength,它取代了OpenReadStream,后者没有响应转换字节的功能,例如: private byte[] ConvertToBytes(IFormFile image) { byte[] imageBytes = null; //Then, Read stream in Binary
private byte[] ConvertToBytes(IFormFile image)
{
byte[] imageBytes = null;
//Then, Read stream in Binary
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int) image.ContentLength);
return imageBytes;
}
你知道InputStream和ContentLength会发生什么变化吗?在IFormFile中,可以使用OpenReadStream和Length。我不知道如何使用它。有人知道怎么用吗。我希望有类似的代码,如下例所示:
private byte[] ConvertToBytes(IFormFile image)
{
byte[] imageBytes = null;
//Then, Read stream in Binary
BinaryReader reader = new BinaryReader(image.OpenReadStream());
imageBytes = reader.ReadBytes((int) image.Length);
return imageBytes;
}
我在下面的代码中尝试了什么,但由于BinaryReader=new BinaryReader(image.OpenReadStream())显示System.NullReferenceException未被HResult=-2147467261的用户代码处理,因此无法工作
消息=对象引用未设置为对象的实例:
private byte[] ConvertToBytes(IFormFile image)
{
byte[] imageBytes = null;
//Then, Read stream in Binary
BinaryReader reader = new BinaryReader(image.OpenReadStream());
imageBytes = reader.ReadBytes((int) image.Length);
return imageBytes;
}
你能帮我解决这个问题吗
我调用ConvertToBytes将图像文件添加到varbinary数据类型的数据库字段中,请参见下面的代码:
private readonly ApplicationDbContext db = new ApplicationDbContext();
public int UploadImageInDataBase(IFormFile file, PublisherInfosViewModel publisherInfosViewModel)
{
publisherInfosViewModel.Image = ConvertToBytes(file);
var publisherInfos = new PublisherInfos
{
PubInfoId = publisherInfosViewModel.PubInfoId,
PubId = publisherInfosViewModel.PubId,
Title = publisherInfosViewModel.Title,
Image = publisherInfosViewModel.Image,
Description = publisherInfosViewModel.Description,
Contents = publisherInfosViewModel.Contents
};
db.PublisherInfos.Add(publisherInfos);
int i = db.SaveChanges();
if (i == 1)
{
return 1;
}
else
{
return 0;
}
}
如何调用
ConvertToBytes
?当异常发生时,您可以在哪里调用ConvertToBytes
吗?异常发生在BinaryReader=new BinaryReader(image.OpenReadStream());因为OpenReadStream无法允许BinaryReader读取filestream的结果。您是否检查了格式文件
?似乎文件
是空的
…是的,怎么办?通常,OpenReadStream在读取IFormFile文件后应该给出一个值,以便在二进制结果出现时添加一个数据记录(1)。您应该进一步检查如何调用UploadImageInDataBase
,并找出为什么那里没有上载文件
。。。