实现base64转换为Blob再上传
直接上代码
getBlobBydataURI(dataURI, type) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], { type: type });
},
base64Upload(base64, url) {
var $Blob = this.getBlobBydataURI(base64, 'image/png');
var formData = new FormData();
formData.append("file", $Blob, "avatar.png");
var request = new XMLHttpRequest();
request.open("POST", url);
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
console.log("上传成功", request);
} else {
console.log("上传失败,检查上传地址是否正确", request);
}
}
}
request.send(formData);
}
|