IFormFile not being populated by dropzone uploadMu

2019-03-28 08:24发布

问题:

The problem I am having is that the List of IFormFile is not being populated with the given files but when i call HttpContext.Request.Form.Files; then I have access to the files. I would prefer to use IFormFile as it seems to be new Dotnet core 2.0 way of doing things.

I have the following request payload:

With the following request headers:

And Razor pages handler:

public async Task<ActionResult> OnPostSend(ConditionResponse conditionResponse)
    {
        var files = HttpContext.Request.Form.Files;
    }

Condition response model:

public class ConditionResponse
{
    public List<string> Plots { get; set; }

    public string Comments { get; set; }

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

回答1:

After looking at the request from a html5 multiple file upload I noticed the request does not add the indexes to the filename (files[n]). Dropzone.js does this so there is a work around. If you add the paramName option to Dropzone JS config and have it call a method which returns files you will get the same behaviour as the html5 multiple file upload.

function myParamName() {
                return "files";
            }

 Dropzone.options.myDropzone = {
                uploadMultiple: true,
                paramName: myParamName,
}


回答2:

The accepted answer worked perfectly, I am not sure why and how, but it works. I just wanted to get rid of that additional function, i.e., we can use it like this:

 Dropzone.options.myDropzone = {
                uploadMultiple: true,
                paramName: () => "files",
}

Or if old browsers are also targeted:

 Dropzone.options.myDropzone = {
                uploadMultiple: true,
                paramName: function () { "files" },
}