Vue结合webuploader实现文件分片断点续传
- 基于 WebUploader,实例化 WebUploader,参数见文中注释
this.uploader = WebUploader.create({
...
})
- 注册 WebUploader
new WebUploader.Uploader.register({
beforeSend: (block) => {
},
afterSendFile: () => {
}
})
- 组件监听
this.uploader.on('fileQueued', (file) => {
const that = this
file.onUploadFileErr = that.uploadContCommon.onUploadFileErr
file.onUploadFileSuccess = that.uploadContCommon.onUploadFileSuccess
file.callbackDate = that.uploadContCommon.callbackDate
that.uploader.sort(function(a, b) { return a.size - b.size })
var uniqueFileName = md5(file.name + file.size + file.type + that.formData.userId)
that.uniqueFileNameMap.set(file.id, uniqueFileName)
that.uploader.makeThumb(file, function(error, ret) {
if (error) {
} else {
}
})
that.$emit('fileChange', file)
})
this.uploader.on('uploadStart', (file) => {
})
this.uploader.on('uploadProgress', (file, percentage) => {
this.$emit('progress', file, percentage)
})
this.uploader.on('uploadSuccess', (file, response) => {
})
this.uploader.on('uploadError', (file, reason) => {
this.$emit('uploadError', file, reason)
})
this.uploader.on('error', (type) => {
let errorMessage = ''
if (type === 'F_EXCEED_SIZE') {
errorMessage = `文件大小不能超过${this.fileSingleSizeLimit / (1024 * 1000)}M`
} else if (type === 'Q_EXCEED_NUM_LIMIT') {
errorMessage = '文件上传已达到最大上限数'
} else {
errorMessage = `上传出错!请检查后重新上传!错误代码${type}`
}
this.$emit('error', errorMessage)
})
this.uploader.on('uploadComplete', (file, response) => {
this.$emit('complete', file, response)
})