0

in the code below i'm fetching a .txt file and it is getting encoded without giving an error so oi want to use the same thing and also convert an image into 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);
}
Bill
  • 3,382
  • 20
  • 41
Rishab Gupta
  • 53
  • 1
  • 1
  • 3
  • 1
    possible duplicate of [Get image data in JavaScript?](http://stackoverflow.com/questions/934012/get-image-data-in-javascript) – Ross Taylor-Turner Nov 19 '14 at 14:35
  • Check this http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript – Sreeraj Nov 19 '14 at 14:38
  • the code above which i've written is for the use in thunderbird for sending an attachment . so my text file is getting attached to the mail but i'm unable to figure out hoe to encode the image selected as attachment. – Rishab Gupta Nov 19 '14 at 14:40
  • Jack Sparrow i want to do it without the use of canvas or anything related to HTML ... i want to fetch a file from my dialog box and simply encode it to base64 – Rishab Gupta Nov 19 '14 at 14:44
  • duplicate of [How can you encode a string to Base64 in JavaScript?](http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript/247261) – dusky Nov 19 '14 at 15:09

0 Answers0