在下面的代码中我提取了一个.txt文件并且它在没有出错的情况下进行编码所以我想使用相同的东西并将图像转换为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 a 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 to 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);
}