refine imageCropper fetch

This commit is contained in:
Pan 2017-05-15 22:24:29 +08:00
parent 0476f25eba
commit 69fe4d1e91
1 changed files with 14 additions and 34 deletions

View File

@ -105,6 +105,7 @@
<script> <script>
/* eslint-disable */ /* eslint-disable */
import {effectRipple, data2blob} from './utils'; import {effectRipple, data2blob} from './utils';
import fetch from 'utils/fetch';
import langBag from './lang'; import langBag from './lang';
const mimes = { const mimes = {
'jpg': 'image/jpeg', 'jpg': 'image/jpeg',
@ -659,7 +660,8 @@
}) })
} }
// //
const uploadProgress = function(event) { function uploadProgress (event) {
console.log(event)
if (event.lengthComputable) { if (event.lengthComputable) {
that.progress = 100 * Math.round(event.loaded) / event.total; that.progress = 100 * Math.round(event.loaded) / event.total;
} }
@ -669,43 +671,21 @@
that.loading = 1; that.loading = 1;
that.setStep(3); that.setStep(3);
that.$emit('crop-success', createImgUrl, field, ki); that.$emit('crop-success', createImgUrl, field, ki);
new Promise(function(resolve, reject) { fetch({
let client = new XMLHttpRequest(); url,
client.open('POST', url, true); method: 'post',
client.onreadystatechange = function() { data: fmData
if (this.readyState !== 4) { }).then(resData=>{
return; that.loading = 2;
} that.$emit('crop-upload-success', resData.data);
if (this.status === 200) { }).catch(err=>{
resolve(JSON.parse(this.responseText)); if (that.value) {
} else {
reject(this.status);
}
};
client.upload.addEventListener("progress", uploadProgress, false); //
// header
if (typeof headers == 'object' && headers) {
Object.keys(headers).forEach((k) => {
client.setRequestHeader(k, headers[k]);
})
}
client.send(fmData);
}).then(
//
function(resData) {
that.loading = 2;
that.$emit('crop-upload-success', resData);
},
//
function(sts) {
if (that.value) {
that.loading = 3; that.loading = 3;
that.hasError = true; that.hasError = true;
that.errorMsg = lang.fail; that.errorMsg = lang.fail;
that.$emit('crop-upload-fail', sts, field, ki); that.$emit('crop-upload-fail', err, field, ki);
} }
} });
);
} }
} }
} }