0

I want to select all images

state = {
    file: null
}
handleFile(e) {
    let file = e.target.files
    this.setState({ file: file })
    console.log('handle file:', file)
}

if i use index like this, then i can only upload single image

let file = e.target.files[0]

but i want to select all index at once and upload it ! i can upload multiple images with POSTMAN but not react.

this is the index which i want to select all at once

i want to select all index and use it in within

let file = e.target.files  <----here

not like this !

let file = e.target.files[0] or [1] or [2] ....

how to select all index at once ? thank you in advance :)

Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
Titan
  • 244
  • 1
  • 4
  • 18
  • What does it mean "select all index at once", an array of files is a "selection" for you? Did you read about `#Array.map`? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map – Dennis Vash Jul 25 '20 at 08:38
  • Im sorry for inconvenience. im asking how to select all those index without just choosing single index of that images? in another words how to select all images with those indexes? – Titan Jul 25 '20 at 08:44

1 Answers1

0

e.target.files is just an array, .forEach should work well

You could also use array in the state

state = {
    files: []
}
handleFile(e) {
    let files = e.target.files
    let filesUploading = [];
    files.forEach((file, idx) => {
        filesUploading.push({
            id: idx,
            name: file.name,
            file: file
        })
    })
    this.setState({ files: filesUploading })
}

For the actual upload, check answers here Multiple file upload with reactjs

Sergey Khmelevskoy
  • 2,429
  • 3
  • 19
  • 43