近来在做一个项目,须要在前端对图片切片并上传到七牛云
手艺要点
- canvas.toBlob(blob=>{}); //可将canvas保留成二进制文件
- formData.append(‘file’, blob, ‘filename’); //将二进制文件添加到FormData中
- ajax.send(formData); //上传数据到后端处置惩罚
代码完成
现在没有看到七牛云供应的JSSDK支撑上传blob到云上的, 所以这里本身完成了一下
注重ajax post formData到七牛云的时刻不要设置content-type,让浏览器本身处置惩罚
class Qiniu {
constructor(options = {}) {
this._options = {...options};
}
ajax(url = '', opt = {}) {
const options = {method: 'GET', async: true, dataType: 'JSON', ...opt};
return new Promise((resolve, reject) => {
const ajax = this.createAjax();
if (ajax) {
const _async = typeof options.async === 'boolean' ? options.async : true;
ajax.open(options.method || 'GET', url, _async);
if (options.headers) {
Object.keys(options.headers).forEach(key => {
ajax.setRequestHeader(key, options.headers[key]);
});
}
ajax.onreadystatechange = () => {
if (ajax.readyState === 4) {
if (ajax.status >= 200 && ajax.status <= 400) {
let res = ajax.responseText;
if (options.dataType && options.dataType.toUpperCase() === 'JSON') {
res = JSON.parse(res);
}
resolve(res, ajax.response);
} else {
reject(new Error('要求失利:' + ajax.status))
}
}
};
ajax.send(options.data);
} else {
reject(new Error('建立Ajax要求失利!'));
}
});
}
createAjax() {
let xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
getToken() {
if (this._options.token) {
return Promise.resolve(this._options.token);
} else {
if (typeof this._options.getToken === 'function') {
const t = this._options.getToken();
if (t && typeof t.then === 'function') {
return t.then(token => {
this._options.token = token;
return token;
})
} else if (typeof t === 'string') {
this._options.token = t;
return Promise.resolve(t);
} else {
return Promise.reject(new Error('options.getToken必需返回Promise或string token'));
}
} else if (typeof this._options.getTokenUrl === 'string') {
return this.ajax(this._options.getTokenUrl, {
headers: {
authorization: this._options.authorization
}
}).then(res => {
this._options.token = res.uptoken;
return this._options.token;
}).catch(ex => {
throw new Error('猎取uptoken失利:' + ex.message);
});
} else {
return Promise.reject(new Error('没法猎取token'));
}
}
}
upload(file, filename, key) {
return this.getToken().then(token => {
const formData = new FormData();
formData.append('key', key || filename);
formData.append('token', token);
formData.append('file', file, filename);
return formData;
}).then(data => {
//注重不要设置content-type,浏览器自动会添补
return this.ajax('http://upload.qiniu.com/', {
method: 'POST',
data,
});
});
}
}
canvas.toBlob(blob => {
const filename = Date.now() + Math.random() + '.png';
const qiniu = new Qiniu({getTokenUrl:'这里写猎取七牛token的接口地点'});
qiniu.upload(blob, filename);
}, 'image/png');