Answer the question
In order to leave comments, you need to log in
How to read image from file in BASE64?
Alas, I'll forgive you not to laugh at my code. The task is that - there are images, local. It would be desirable to read them and to thrust in the form of base64. Lack of JS experience resulted in the following broken snippet:
function image_ref() {
var images = document.querySelectorAll('img');
for (i=0; i<images.length; i++) {
image = images[i];
var attr = image.src;
if (attr.toString().startsWith('file')) {
console.log(attr)
var reader = new FileReader();
reader.onloadend = function (event) {
image.src = reader.result
}
reader.readAsDataURL(attr);
}
}
Answer the question
In order to leave comments, you need to log in
I decided to do this:
1. After inserting the content into the editor, I get the local addresses of the images.
2. Added a test field where file paths will be thrown in the form C:\Users\MT_SCA~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg
3. The user will have to copy the contents to the clipboard.
4. Then a file open dialog opens
5. there with a combination of ctrl + v file names
6. then everything is read by FileReader'om
Here is the encoder-decoder - foxtools.ru/Base64 But the main thing is there is an API.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question