31

I have a list of file object like:

var files = [File, File, File]

I want to convert it to something like:

I have a file field where I want to map my files as FileList object.

var uploading_files = {}
files.forEach(function(file, key) {
    uploading_files[key] = file

})
var uploader = document.getElementById('uploader')
uploader.files = uploading_files

console.log(uploader.files)

But It does not give me FileList object ? How can I create a file list objecdt ? Thanks in advance..

varad
  • 7,309
  • 20
  • 60
  • 112
  • what is the error in your console log? – Naga Sai A Jul 19 '16 at 04:11
  • It gives me emptly FileList Object.. No files assigned to it – varad Jul 19 '16 at 04:24
  • 1
    At the end how did you accomplish it? I am facing same issue as of now. – ateet Sep 14 '20 at 11:08
  • I'm also interested to know if you ever accomplished this. – Randall Coding Jan 18 '22 at 19:46
  • I am working with a similar situation. You can iterate through each element in the array of files, and append to a form object, with a custom id, say a relative file path. Using Fetch API you can easily upload files with other form contents and fetch the list of files using $_FILES to iterate through each key, and upload the files to the server or database. It is worth noting that You must use the actual File Object when using the FormData API and store each file seperately. On the server side it should appear as a FileList. So you may implement your own FileList object to implement this – Brandon Roberts Feb 05 '22 at 18:35

1 Answers1

39

The FileList object is "read-only" and has no implementation of its constructor.

Visit this page: FileList On MDN

Timur Gilauri
  • 824
  • 8
  • 13