jinjian1.1/utils/imgtobase.js

20 lines
481 B
JavaScript
Raw Normal View History

2024-11-05 16:29:09 +08:00
//图片转base64
export function getBase64Image(src) {
return new Promise(resolve => {
let xhr = new XMLHttpRequest();
xhr.open('get', src, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
let blob = this.response;
let oFileReader = new FileReader();
oFileReader.onload = function(e) {
const base64 = e.target.result;
resolve(base64);
};
oFileReader.readAsDataURL(blob);
}
};
xhr.send();
});
}