1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| upload() { let that = this, { lang, imgFormat, mime, url, params, headers, field, ki, createImgUrl, withCredentials } = this, fmData = new FormData(); fmData.append(field, data2blob(createImgUrl, mime), field + '.' + imgFormat);
if (typeof params == 'object' && params) { Object.keys(params).forEach((k) => { fmData.append(k, params[k]); }); }
const uploadProgress = function(event) { if (event.lengthComputable) { that.progress = 100 * Math.round(event.loaded) / event.total; } };
that.reset(); that.loading = 1; that.setStep(3); new Promise(function(resolve, reject) { let client = new XMLHttpRequest(); client.open('POST', url, true); client.withCredentials = withCredentials; client.onreadystatechange = function() { if (this.readyState !== 4) { return; } if (this.status === 200 || this.status === 201) { resolve(JSON.parse(this.responseText)); } else { reject(this.status); } }; client.upload.addEventListener('progress', uploadProgress, false); if (typeof headers == 'object' && headers) { Object.keys(headers).forEach((k) => { client.setRequestHeader(k, headers[k]); }); } client.send(fmData); }).then( function(resData) { if (that.value) { that.loading = 2; that.$emit('crop-upload-success', resData, field, ki); } }, function(sts) { if (that.value) { that.loading = 3; that.hasError = true; that.errorMsg = lang.fail; that.$emit('crop-upload-fail', sts, field, ki); } }); } }
|