Vins
Vins

Reputation: 411

IFormFile IEnumerable is of size 1 even though multiple file upload is specified

Model

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

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

View

<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);
                    }......

Whenever I try to upload more than one file, the IEnumerable only takes the first file and leaves the rest behind.

Choose Files, 3 Files chosen

Translated: Choose Files, 3 Files

Even though I specified the multiple file upload in the input field, the certificates.IFormFiles delivers me a size of 1.

enter image description here

What am I doing wrong?

Upvotes: 2

Views: 1446

Answers (2)

Vins
Vins

Reputation: 411

After a few discussions with the team, I found out that the custom property attribute data-file-dragndrop only allows 1 file to be sent via AJAX. You were still able to upload more than one file but the AJAX-Request only accepted one file. In the case where you uploaded more than one file, the AJAX-Request took the first file and left the rest behind.

We all did not know this until the one who created this attribute explained it to us. I'm sorry for the inconvenience!

Upvotes: 0

Alaaeddine HFIDHI
Alaaeddine HFIDHI

Reputation: 886

If you used .Net core 2.0 or 2.1, try to update your SDK to 2.2.203 then it will work without problem.
the issue not in your code, it was a bug in .NET core
I invite you to read more about this bug here : https://github.com/aspnet/Mvc/issues/8527

Upvotes: 1

Related Questions