convert file to base64 in javascript

علی ذوالفقار
1401/11/15 21:02:47 (376)
        const input = document.getElementById("MyFileInput");

        input.addEventListener("change", async (e) => {
            console.log(e.target.files[0]); // name , type , size and more info about file 
            const base64 = await b64(e.target.files[0]);
        });

        function b64(file){
            const reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                console.log({ fileData: reader.result, name : file.name });
            };

            reader.onerror = function () {
                console.log(reader.error);
            };
        }

Back