c# - IFormFile IEnumerable 的大小为 1,即使指定了多个文件上传

标签 c# asp.net-mvc file-upload

模型

    public List<ZertifikatFiles> Files { get; set; }

    [NotMapped]
    public IEnumerable<IFormFile> Certificates { get; set; }

查看

<form asp-action="AddCertificate" method="post" enctype="multipart/form-data"  data-file-dragndrop>
<div class="row">
    <div class="col-md-3"></div>
    <div class="form-group col-md-9">
        <input type="file" asp-for="IFormFiles" multiple />
        <span asp-validation-for="IFormFiles" class="text-danger"></span>
    </div>
</div>  </form>

Controller

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AddCertificate(Certificates certificates )
    {
        if (ModelState.IsValid)
        {
            if (certificates.IFormFiles != null && !certificates.IFormFiles.IsEmpty())
            {
                certificates.Files = new List<CertificateFiles>();
                foreach (IFormFile formFile in certificates.IFormFiles)
                {
                    byte[] bytes = new byte[formFile.Length];
                    using (var reader = formFile.OpenReadStream())
                    {
                        await reader.ReadAsync(bytes, 0, (int)formFile.Length);
                    }......

每当我尝试上传多个文件时,IEnumerable 只会获取第一个文件而将其余文件留在后面。

Choose Files, 3 Files chosen

已翻译:选择文件,3 个文件

即使我在输入字段中指定了多个文件上传,certificates.IFormFiles 给我的大小也是 1。

enter image description here

我做错了什么?

最佳答案

如果您使用的是 .Net core 2.0 或 2.1,请尝试将您的 SDK 更新到 2.2.203,然后它将正常运行。
问题不在你的代码中,它是 .NET 核心中的错误
我邀请您在此处阅读有关此错误的更多信息:https://github.com/aspnet/Mvc/issues/8527

关于c# - IFormFile IEnumerable 的大小为 1,即使指定了多个文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594590/

相关文章:

c# - 使用 .NET 连接 2 台计算机

c# - 发现多个类型与名为 'Home' 的 Controller 匹配 - 在两个不同的区域

c# - 基于 Linq 构建复杂搜索过滤器

javascript - 如何将用户上传的文件定向到特定的 Google 云端硬盘文件夹

angularjs - 使用输入表单将数据上传到 Yesod 服务器

c# - 使用 GroupBy 消除 List<T> 中的重复对象

c# - 每个请求的 WebApi、Autofac、System.Web.Http.Filters.ActionFilterAttribute 实例

asp.net-mvc - 在MVC中使用Session对象,真的很糟糕吗?

asp.net-mvc - 如何在 Entity Framework 中为GUID设置NewId()

javascript - Multer 和 AngularJS