如何在vue组件的load函数中返回工作?

时间:2018-03-08 14:46:34

标签: javascript vue.js vuejs2 image-uploading vue-component

我有这样的vue组件:

<template>
    <section>
        ...
    </section>
</template>
<script>
    export default {
        ...
        data() {
            return {
                allowableTypes: ['jpg', 'jpeg', 'png'],
                maximumSize: 4000000
            }
        },
        methods: {
            ...
            onFileChange(e) {
                if (this.validate(e.target.files[0])) {
                    let files = e.target.files,
                        reader = new FileReader()
                    // if any values
                    if (files.length){
                        reader.onload = (e) => {
                            this.image = e.target.result
                        }
                        reader.readAsDataURL(files[0])
                    }
                }
            },
            validate(image) {
                // validation file type  
                if (!this.allowableTypes.includes(image.name.split(".").pop().toLowerCase())) {
                    return false
                }
                // validation file size
                if (image.size > this.maximumSize) {
                    return false
                }
                // validation image resolution
                let img = new Image()
                img.src = window.URL.createObjectURL(image)
                let self = this
                img.onload = function() {
                    let width = img.naturalWidth,
                        height = img.naturalHeight

                    window.URL.revokeObjectURL(img.src)

                    if(width != 850 && height != 350) {
                        return false
                    }
                }
                return true
            }
        }
    }
</script>

如果用户上传图片,则会调用onFileChange方法。在显示图像之前,它将调用方法验证以验证图像。

我尝试验证文件大小和文件类型,但它确实有效。这里的问题是验证解决方案。

从我的代码来看,似乎我的代码是真的

但是当我尝试这样的时候:

我从代码上传width = 100height = 100的图片,如果它返回`false``。

但是当我运行我的代码时,它会返回true

似乎返回在img.onload

中无效

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

处理异步验证的一个好方法是使用Promises:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

如果您的目标是Internet Explorer,请务必使用如下所示的polyfill:
https://github.com/stefanpenner/es6-promise
您的代码将如下所示:

onFileChange(e) {
  let self = this
  this.validate(e.target.files[0])
    .then(function() {
      let files = e.target.files,
        reader = new FileReader()
      // if any values
      if (files.length) {
        reader.onload = (e) => {
          self.image = e.target.result
        }
        reader.readAsDataURL(files[0])
      }
    })
    .catch(function() {
      // do something in the case where the image is not valid
    })
},
validate(image) {
  let self = this
  return new Promise(function(resolve, reject) {
    // validation file type
    if (!self.allowableTypes.includes(image.name.split(".").pop().toLowerCase())) {
      reject()
    }
    // validation file size
    if (image.size > self.maximumSize) {
      reject()
    }
    // validation image resolution
    let img = new Image()
    img.src = window.URL.createObjectURL(image)
    img.onload = function() {
      let width = img.naturalWidth,
        height = img.naturalHeight

      window.URL.revokeObjectURL(img.src)

      if (width != 850 && height != 350) {
        reject()
      } else {
        resolve()
      }
    }
  })
}

如果您不想或不能使用Promises,您可以使用Callback来实现相同的行为:

onFileChange(e) {
  let self = this
  let validCallback = function() {
    let files = e.target.files,
      reader = new FileReader()
    // if any values
    if (files.length) {
      reader.onload = (e) => {
        self.image = e.target.result
      }
      reader.readAsDataURL(files[0])
    }
  }
  let unvalidCallback = function() {
    // do something in the case where the image is not valid
  }
  this.validate(e.target.files[0], validCallback, unvalidCallback)

},
validate(image, validCallback, unvalidCallback) {
  // validation file type
  if (!this.allowableTypes.includes(image.name.split(".").pop().toLowerCase())) {
    unvalidCallback()
    return
  }
  // validation file size
  if (image.size > this.maximumSize) {
    unvalidCallback()
    return
  }
  // validation image resolution
  let img = new Image()
  img.src = window.URL.createObjectURL(image)
  let self = this
  img.onload = function() {
    let width = img.naturalWidth,
      height = img.naturalHeight

    window.URL.revokeObjectURL(img.src)

    if (width != 850 && height != 350) {
      unvalidCallback()
      return
    } else {
      validCallback()
    }
  }
}

答案 1 :(得分:0)

onloadend而非onload

将您的代码更改为:

let self = this;     

var reader = new FileReader(); 
reader.onloadend = () => {
 // you logic here (use self, not this)
}