1

Below code is used to display the image in new tab if the extension is jpg.

Similarly, I want to write a text document (from base64 string to text) in new tab if the extension is txt.

success: function (data) {
            var extension = fileName.split('.').pop();
            if (extension == "jpg") {
                var image = new Image();
                image.src = "data:image/jpg;base64," + data.d;
                var w = window.open("");
                w.document.write(image.outerHTML);
            }
            else if(extension == "txt") {

            }
    }
Earth
  • 3,477
  • 6
  • 37
  • 78
  • possible duplicate of [How do I write content to another browser window using Javascript?](http://stackoverflow.com/questions/169833/how-do-i-write-content-to-another-browser-window-using-javascript) – Sadikhasan Jan 07 '15 at 06:07
  • @Sadikhasan, That question not mentioned about the conversion of base64 string to text – Earth Jan 07 '15 at 06:08
  • @JohnStephen what is not working? there is no code in your else if condition. – Jai Jan 07 '15 at 06:09
  • How to convert the base64 string to txt format? – Earth Jan 07 '15 at 06:11
  • You don't give enough context in your question, a full working example which includes how the file is read by the browser and processed would be helpful. – chiliNUT Jan 07 '15 at 06:14

1 Answers1

2

Base64 images are more trivial because you just have to stick the base64 into the image source, other things like text need to be decoded. See https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

success: function (data) {
            var extension = fileName.split('.').pop();
            if (extension == "jpg") {
                var image = new Image();
                image.src = "data:image/jpg;base64," + data.d;
                var w = window.open("");
                w.document.write(image.outerHTML);
            }
            else if(extension == "txt") {
                var w = window.open("");
                w.document.write(window.atob(data.d));
            }
    }
chiliNUT
  • 18,989
  • 14
  • 66
  • 106