0

I want to download text file using javascript. I have tried with many scenarios but didn't get luck. Here is one example:

(function() {
  var textFile = null,
    makeTextFile = function(text) {
      var data = new Blob([text], {
        type: 'text/plain'
      });

      // If we are replacing a previously generated file we need to
      // manually revoke the object URL to avoid memory leaks.
      if (textFile !== null) {
        window.URL.revokeObjectURL(textFile);
      }

      textFile = window.URL.createObjectURL(data);

      return textFile;
    };


  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');

  create.addEventListener('click', function() {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();
<textarea id="textbox">Type something here</textarea>
<button id="create">Create file</button> 
<a download="info.txt" id="downloadlink" style="display: none">Download</a>

please help.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • 2
    Please don't use tricks like this to circumvent the 'code in the question' policy. If jsFiddle goes down your question will be unanswerable. Please edit it to include all relevant code. – Rory McCrossan Jan 10 '17 at 07:32
  • Also, which version of IE specifically? `Blob` is unsupported in – Rory McCrossan Jan 10 '17 at 07:34
  • 2
    @JituJoshi Agreed with Rory as per first comment. This time i have done it for you but make a practice to add the relevant code in the question itself. – Jai Jan 10 '17 at 07:35
  • All versions of Internet Explorer. – Jitendra Joshi Jan 10 '17 at 07:36
  • Have you checked the console for errors or determined which part of the code specifically is causing the issue? Your title mentions `encodeURIComponent`, yet that's not called at all in the sample you provided...? – Rory McCrossan Jan 10 '17 at 07:38
  • @JituJoshi That's a joke, IE4-5-6-7-8 (looking back at what we have now, compared to when they were released, ofcourse) were absolutely horrible, if you support a browser, never ever give IE support for any version below 10 ;) (3 versions down) – Icepickle Jan 10 '17 at 07:39
  • Possible duplicate of [IE download file](http://stackoverflow.com/questions/25121384/ie-download-file) – Kaiido Jan 10 '17 at 08:05

2 Answers2

0

Thank you all for response. I have found solution.

function download(data, filename, type) {
    var a = document.createElement("a"),
        file = new Blob([data], { type: type });
    if (window.navigator.msSaveOrOpenBlob) // IE10+
        window.navigator.msSaveOrOpenBlob(file, filename);
    else { // Others
        var url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function () {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
    }
}
0

A very fast and easy solution is to use FileSaver.js : https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js

Then it takes only 2 lines of code to download a txt file :

var blob = new Blob(["Hello World"], {type: "text/plain;charset=utf-8"});

saveAs(blob, "filename.txt");

This code example will display a dialog box to download a file named "filename.txt" containing the text "Hello world". Just replace this by the file name and the text content of your choice !

Chaitanya Kumar
  • 212
  • 2
  • 13