Heim > Fragen und Antworten > Hauptteil
So laden Sie Front-End-Base64-Bilder in Alibaba Cloud oss hoch
習慣沉默2017-07-05 10:28:13
你现在是获取到了base64的图片了吗 如果是的话 那就调用oss对应的上传接口不就好了 如果没有获取到base64格式的图片 就用下面的办法:
init : function(options) {
var oThis = this;
if( typeof FileReader==='undefined' ) {
this.imgBox.innerHTML = "抱歉,你的浏览器不支持 FileReader";
this.file.setAttribute('disabled','disabled');
} else {
this.file.addEventListener('change', oThis.readFile.bind(this), false);
}
},
readFile : function(event) {
var file = this.file.files[0],
oThis = this;
console.log(this);
if ( !oThis.reg.test(file.type) ) {
alert("文件必须为图片!");
return;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
var img = new Image(),
length = this.result.length,
result = this.result;
img.src = result;
img.onload = function () {
if ( length > oThis.maxSize ) {
result = oThis.compress(img);
}
oThis.imgBox.src = result;
oThis.cb(result);
};
}
},
compress : function(img) {
var width = img.width,
height = img.height,
data = null;
this.canvas.width = img.width;
this.canvas.height = img.height;
this.ctx.drawImage(img, 0, 0, width, height);
data = this.canvas.toDataURL('image/jpeg', this.ratio);
return data;
}