getImgSize(str) {
//获取base64图片大小,返回KB数字
var str = base64url.replace('data:image/jpeg;base64,', '');//这里根据自己上传图片的格式进行相应修改 var strLength = str.length; var fileLength = parseInt(strLength - (strLength / 8) * 2); // 由字节转换为KB var size = ""; size = (fileLength / 1024).toFixed(2); return parseInt(size); },
base64:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZxxxxxxxkopKACiiigApaSloA/9k=
// 上传图片
function upload(image) {
var imgFile=dataURLtoFile(image,"img.png");
var xhr=new XMLHttpRequest();
var fd=new FormData();
xhr.open('POST','/springStudy/user/multifiles');
fd.append("multipartFile",imgFile);
xhr.send(fd);
console.log('上传json!')
}
//将图片Base64 转成文件
function dataURLtoFile(dataurl, filename) {
console.log("转文件")
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}