IFormFile 'System.InvalidOperationException'

IFormFile 'System.InvalidOperationException'

我的 ASP.NET 核心应用程序 IFormFile 中的非 public.member _baseStream 属性在上传文件后抛出以下异常:

ReadTimeout =   ((Microsoft.AspNetCore.Http.Internal.FormFile)BildUpload)._baseStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

我正在尝试使用带有以下代码的剃刀页面上传文件:

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <input type="file" name="BildUpload"  />
    </div>
    <input type="submit" value="Upload" class="btn btn-default" />
</form>

在代码隐藏中 class 除了 razor 页面,我只得到了声明,没有其他任何东西读取或写入参数:

    public IFormFile BildUpload { get; set; }

感谢您的帮助!

我的最终目标是将文件解析为字节数组并将 sava at 解析为数据库,如下所示: 但是在那里我得到了一个空指针异常。

您可以阅读文件上传 here 以确保您没有错过任何步骤,以防您需要更多关于文件上传的知识。

以防有人遇到类似行为。这是我上传图像并将其转换为字节数组(用于将其存储在 Microsoft SQL 数据库中)的解决方案:

首先使用问题中的 xaml 代码和 IFormFile 属性。 在后面的代码中添加此代码以将表单数据处理为字节数组:

public async Task<IActionResult> OnPostAsync(string id)
{
    //get the "Kontakt" entity
    if (!await SetKontaktAsync(id))
    {
          return NotFound();
    }

    //convert form data to byte array and assign it to the entity
    if (BildUpload.Length > 0)
    {
          using (var ms = new MemoryStream())
          {
                BildUpload.CopyTo(ms);
                Kontakt.Bild = ms.ToArray();
          }
    }

    //save changes to the database
    _context.Attach(Kontakt).State = EntityState.Modified;
    await _context.SaveChangesAsync();

    //reload page
    return await OnGetAsync(Kontakt.GID);
}

框架:ASP.NET 核心 2.2,Entity Framework 核心 2.2