search

Home  >  Q&A  >  body text

html5 - How does FileReader read multiple files at once?

<input type="file" name="sendfile" id="sendfile" v-show='false' accept="image/png,image/gif,image/jpeg" @change='upload' multiple>

As above, for an input that supports multiple image uploads, how to use filereader to locally read the dataurl of each image? How to write this upload?

曾经蜡笔没有小新曾经蜡笔没有小新2765 days ago805

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-06-07 09:26:35

    Loop reading

    new Vue({
      el: 'app',
      methods: {
        async upload () {
          const files = event.target.files
          const uploadList = []
          console.log(files)
    
          const readFileAsync = file => new Promise(resolve => {
            const reader = new FileReader()
            reader.onload = evt => resolve(evt.target.result)
            reader.readAsDataURL(file)
          })
    
          for (let i = 0; i < files.length; i++) {
            uploadList.push(await readFileAsync(files[i]))
          }
    
          event.target.value = null
    
          console.log(uploadList)
        }
      }
    })
    

    reply
    0
  • Cancelreply