Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -71,5 +71,8 @@
"vue-style-loader": "^1.0.0",
"webpack": "^1.13.1",
"webpack-stream": "^3.2.0"
},
"directories": {
"example": "example"
}
}
30 changes: 20 additions & 10 deletions upload-2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export default {
method: {
type: String,
'default': 'POST'
},
beforeUpload: {
type: Function,
'default': () => {}
}
},
data() {
Expand Down Expand Up @@ -780,22 +784,21 @@ export default {
ctx.drawImage(sourceImg, x / scale, y / scale, width / scale, height / scale);
that.createImgUrl = canvas.toDataURL(mime);
},
prepareUpload(){
async prepareUpload(){
let {
url,
createImgUrl,
field,
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,
Expand All @@ -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) {
Expand All @@ -820,6 +828,8 @@ export default {
})
}

fmData.append(field, data2blob(createImgUrl, mime), field + '.' + imgFormat);

// 监听进度回调
const uploadProgress = function(event) {
if (event.lengthComputable) {
Expand Down Expand Up @@ -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);
}
},
// 上传失败
Expand All @@ -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);
}
}
);
Expand Down