4
In Javascript, when selecting return the selected values from a file input[type=file]
, it returns the values within a FileList
.
Example:
document.querySelector('#files').addEventListener('change', function () {
console.log(this.files.constructor); // Isso não é um Array, é um FileList
console.log(this.files)
})
<div>Selecione um ou mais arquivos:</div>
<input type="file" multiple id="files" />
The problem is that the FileList
has no common operations of a Array
normal, like the forEach
, map
and the like.
Behold:
document.querySelector('#files').addEventListener('change', function () {
console.log('Filelist:', this.files.forEach); // retorna 'undefined'
console.log('Array:', [].forEach); // retorna a função
})
<input type="file" multiple id="files" />
I wonder if it is possible to convert FileList
in a Array
common in Javascript.
Did you just try to apply array methods to Filelist? For example,
const fileNames = Array.prototype.map.call(fileListInstance, (file) => file.name);
.– bfavaretto
I was hoping you’d answer that @bfavaretto ;)
– Wallace Maxters