[英]How to convert image into base64?
I have seen many tutorials about converting image to base64. 我看过很多有关将图像转换为base64的教程。 The work I did is based on that. 我所做的工作就是基于此。 I have created a function inside which this conversion will take place. 我创建了一个将在其中进行转换的函数。 ButI need to pass image like below function and get console.log like below. 但是我需要像下面的函数那样传递图像,并像下面那样获取console.log。
I am not sure how to do it? 我不确定该怎么做?
getPDF() {
let image = "assets/icon/logo.png";
let imageData = this.getBase64Image(image);
console.log("imageData", imageData);
}
getBase64Image(img) {
let xhr = new XMLHttpRequest();
xhr.open("GET", img, true);
xhr.responseType = "blob";
xhr.onload = function (e) {
console.log(this.response);
var reader = new FileReader();
reader.onload = function (event) {
var res = event.target["result"];
}
var file = this.response;
reader.readAsDataURL(file)
};
xhr.send();
}
To receive the base64 value in the getPDF
function you can use promises. 要在getPDF
函数中接收base64值,可以使用promises。
getPDF() {
let image = "assets/icon/logo.png";
this.getBase64Image(image).then(imageData => {
console.log("imageData", imageData);
}).catch(err => {
console.log(err);
});
}
getBase64Image(img) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open("GET", img, true);
xhr.responseType = "blob";
xhr.onload = function (e) {
console.log(this.response);
var reader = new FileReader();
reader.onload = function (event) {
resolve(event.target["result"]);
}
reader.onerror = function(e) {
reject(e);
};
var file = this.response;
reader.readAsDataURL(file)
};
xhr.onerror = function (e) {
reject(e);
}
xhr.send();
});
}
As suggested by @abadalyan, return a Promise from your getBase64Image
. 正如@abadalyan建议,从返回无极getBase64Image
。
Also do not forget to check the status of the response with readyState
and status
and handle any errors using XMLHttpRequest.onerror
and XMLHttpRequest.ontimeout
using the reject
callback of the Promise. 同样不要忘记用readyState
和status
检查响应的status
并使用XMLHttpRequest.onerror
和XMLHttpRequest.ontimeout
使用Promise的reject
回调来处理任何错误。
You also have to check the FileReader
errors with FileReader.onerror
and FileReader.onabort
, or you could use the FileReader.loadend
callback instead of FileReader.load
to make sure the promise always resolves even when you get a read error. 您还必须使用FileReader.onerror
和FileReader.onabort
检查FileReader
错误,或者可以使用FileReader.loadend
回调而不是FileReader.load
来确保即使遇到读取错误,承诺也始终可以解决。
Here is an example with error handling: 这是错误处理的示例:
function getBase64Image(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.onload = () => { if (xhr.readyState === 4 && xhr.status === 200) { const reader = new FileReader(); reader.readAsDataURL(xhr.response); reader.onerror = e => reject(new Error('Reading error.', e)); reader.onabort = e => reject(new Error('Reading abort.', e)); reader.onload = () => resolve(reader.result); } else { reject(request.statusText); } }; xhr.onerror = e => reject(new Error('Network error.', e)); xhr.ontimeout = e => reject(new Error('Timeout error.', e)); xhr.send(); }); } getBase64Image('https://cors-anywhere.herokuapp.com/https://loremflickr.com/100/100').then(image => { console.log(image); document.querySelector('#img1').src = image; }).catch(e => console.error(e.message || e)); getBase64Image('wrong-url').then(image => { console.log(image); document.querySelector('#img2').src = image; }).catch(e => console.error(e.message || e));
<img id="img1" src="" alt="No image yet"> <img id="img2" src="" alt="No image yet">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.