A
A
Alexandra2020-04-14 23:20:25
JavaScript
Alexandra, 2020-04-14 23:20:25

How to send file in base 64 via FormData?

Hello! How to convert an image to base64 using formData? The code shows the standard version, but I need base64

var request = new XMLHttpRequest();
        request.open('POST', 'identity/', true);
        var data = new FormData(document.forms.profileForm);

        var file = document.getElementById('addFile');

        console.log(file.files);

        if (file.files.length) {
            data.append('foto', file.files[0]);
        }
        console.log(data.get('foto'));

        request.onreadystatechange = function() {
            console.log("request.status>" + request.status);
            if (request.readyState === 4 && request.status === 200) {
                console.log("request.responseText>" + request.responseText);
                var arrJson = JSON.parse(request.responseText);
                console.log(arrJson);
            }
            if (request.status == "400") {
                console.log("400" + request.responseText);
                var json = JSON.parse(request.responseText);
            }
        }
        request.send(data);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question