xuyingjie / fragment

Blog Tool
MIT License
1 stars 0 forks source link

Scope & Closures #7

Open xuyingjie opened 8 years ago

xuyingjie commented 8 years ago

https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures

xuyingjie commented 8 years ago
fileChange(event) {
  var files = event.target.files
  var progress = document.getElementById('progress')

  var readAndUpload = (file, index) => {
    var id = Date.now() * 1000 + index
    var reader = new FileReader()
    reader.onload = () => {
      upload(`img/${id}`, reader.result, {file:true,progress}).then(() => {
        this.item.img.push(id)
      })
    }
    reader.readAsArrayBuffer(file)
  }

  if (files.length > 0) {
    [...files].forEach(readAndUpload)
  }
}
for(let i=0; i<files.length; i++) {
  let file = files[i]
  readAndUpload(file, i)
}