how to encode image to base64 in javascript -
in code below i'm fetching .txt file , getting encoded without giving error oi want use same thing , convert image basse 64
// adding attachment function opensavedialog() { var nsifilepicker = components.interfaces.nsifilepicker; var fp = components.classes["@mozilla.org/filepicker;1"].createinstance(nsifilepicker); fp.init(window, "select file", nsifilepicker.modeopen); fp.appendfilters(nsifilepicker.filterhtml | nsifilepicker.filterimages); fp.appendfilters(nsifilepicker.filtertext | nsifilepicker.filterall); var rv = fp.show(); if (rv == nsifilepicker.returnok || rv == nsifilepicker.returnreplace) { var file = fp.file; var path = "file://" + fp.file.path; readtextfile(path); document.getelementbyid('filename').value = file.leafname; } } // encoding text base64 function readtextfile(filepath) { var rawfile = new xmlhttprequest(); rawfile.open("get", filepath, false); rawfile.onreadystatechange = function() { if (rawfile.readystate == 4) { if (rawfile.status == 200 || rawfile.status == 0) { var alltext = rawfile.responsetext; alert(alltext); encodedata = window.btoa(alltext); } } }; rawfile.send(null); }
Comments
Post a Comment