javascriptnode.jsform-datafilelist

How to create array of files to FormData from FileList?


My goal is to create field in formData which contains array of File objects

My code (with example of output in console):

const onChange = (e) => {
        const files = e.target.files // contains FileList itself
        console.log(files) // FileList {0: File, 1: File, length: 2}
        .....
        var _formData = new FormData();
        var filesArr = Array.from(files) // try to cast FileList to Array type
        console.log(filesArr) // (2) [File, File]
        formData.append("files", filesArr) 
        for (var value of _formData.entries()) {
                console.log(value[0], ',', value[1], ',', typeof (value[1]));
                // files , [object File],[object File] , string

                                }

The problem is that I receive object File with type string instead of needed array of files (objects). Are there any ways how can it be casted?


Solution

  • According to MDN, If the field value is different than String or Blob it will be automatically converted to String. because this type of field is always a string.