A
A
Astrohas2017-01-09 15:27:32
JavaScript
Astrohas, 2017-01-09 15:27:32

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);

        }
    }

How to solve it?
UPD:
I'm trying to read an image at an address like 'file://c:/chtototam.jpg' as a blob
upd:2
to be more precise, I want to copy text with images into wisywig from Word. When copying to the clipboard, Word creates image files in the Temp directory and specifies a link to this image in the src attribute. I need to shove this image as base64, because the link to the local resource didn't give up on the server. in general, here ... Tiny MCE can do this in the premium version, so it's possible ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Astrohas, 2017-01-10
@Astrohas

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

A
Andrey Fedorov, 2017-01-09
@aliencash

Here is the encoder-decoder - foxtools.ru/Base64 But the main thing is there is an API.

A
Alexander Taratin, 2017-01-09
@Taraflex

https://github.com/remy/inliner

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question