diff --git a/package.json b/package.json index e8275c5..f6ea4fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "vue-image-crop-upload", - "version": "2.5.0", + "name": "rui-vue-image-crop-upload", + "version": "2.5.18", "description": "a vue plgin for image upload and crop(vue图片剪裁上传插件)", "main": "upload-2.vue", "scripts": { @@ -71,5 +71,8 @@ "vue-style-loader": "^1.0.0", "webpack": "^1.13.1", "webpack-stream": "^3.2.0" + }, + "directories": { + "example": "example" } } diff --git a/upload-2.vue b/upload-2.vue index d4a62be..6579fcb 100644 --- a/upload-2.vue +++ b/upload-2.vue @@ -196,6 +196,10 @@ export default { method: { type: String, 'default': 'POST' + }, + beforeUpload: { + type: Function, + 'default': () => {} } }, data() { @@ -780,7 +784,7 @@ export default { ctx.drawImage(sourceImg, x / scale, y / scale, width / scale, height / scale); that.createImgUrl = canvas.toDataURL(mime); }, - prepareUpload(){ + async prepareUpload(){ let { url, createImgUrl, @@ -788,14 +792,13 @@ export default { ki } = this; this.$emit('crop-success', createImgUrl, field, ki); - if(typeof url == 'string' && url){ - this.upload(); - }else{ - this.off(); - } + + await this.upload(); }, // 上传图片 - upload() { + async upload() { + await this.beforeUpload(this.createImgUrl, this.field, this.ki) + let that = this, { lang, @@ -811,7 +814,12 @@ export default { method } = this, fmData = new FormData(); - fmData.append(field, data2blob(createImgUrl, mime), field + '.' + imgFormat); + + // 检查是否有url + if(typeof url != 'string' || !url){ + this.off(); + return; + } // 添加其他参数 if (typeof params == 'object' && params) { @@ -820,6 +828,8 @@ export default { }) } + fmData.append(field, data2blob(createImgUrl, mime), field + '.' + imgFormat); + // 监听进度回调 const uploadProgress = function(event) { if (event.lengthComputable) { @@ -858,7 +868,7 @@ export default { function(resData) { if (that.value) { that.loading = 2; - that.$emit('crop-upload-success', resData, field, ki); + that.$emit('crop-upload-success', resData, createImgUrl, field, ki, that.off); } }, // 上传失败 @@ -867,7 +877,7 @@ export default { that.loading = 3; that.hasError = true; that.errorMsg = lang.fail; - that.$emit('crop-upload-fail', sts, field, ki); + that.$emit('crop-upload-fail', sts, createImgUrl, field, ki, that.off); } } );