我正在用Vue.js开发一个文件选取器。我想显示选定的文件预览。 我使用FileReaderAPI来实现这一点。我使用FileReader
对象的readAsDataURL
方法将用户选择的文件作为数据url进行读取。在Vue.js组件中使用FileReader API方法
但是我得到一个错误信息说reader.onload
是不是像一个函数:
Uncaught TypeError: reader.onload is not a function
at VueComponent.handleFileChanges
这可能是读者没有定义,下面我上面提到的的FileReader未定义错误。
我如何努力去做做,这是如下:
handleFileChanges (e) {
var reader = new window.FileReader() // if window is not used it says File READER is not defined
reader.onload(function (event) {
// dispatch fileAttached to state UI postEditor with event.target.result as read dataURL
let imageDataURL = event.target.result
this.$store.dispatch('attachedFile', imageDataURL) // or previewFile
})
reader.readAsDataURL(e.target.files[i])
}
要点是什么我失踪?