Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IFormFile not being populated by dropzone uploadMultiple request

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: request payload

With the following request headers: 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; }
}
like image 389
LiverpoolOwen Avatar asked Mar 28 '18 08:03

LiverpoolOwen


2 Answers

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,
}
like image 91
LiverpoolOwen Avatar answered Oct 27 '22 01:10

LiverpoolOwen


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" },
}
like image 33
Mohammed Noureldin Avatar answered Oct 27 '22 00:10

Mohammed Noureldin