7

I am trying to create a text file using JavaScript, I know it is possible by using ActiveX object, but it runs well only on IE browsers.

My requirement is to generate a text file using JavaScript for a Safari browsers?

Can anyone help me in this regard?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
nagendra
  • 161
  • 1
  • 1
  • 12
  • 1
    Dude provide us your code so that we can help..... – Awais Qarni Nov 18 '11 at 07:00
  • see also [How to create, save a txt file with javascript compatible cross-browser](http://stackoverflow.com/questions/7392117/how-to-create-save-a-txt-file-with-javascript-compatible-cross-browser) – harpo Nov 18 '11 at 07:09

4 Answers4

14

Another way to do it would be to use a Blob and URL.createObjectURL. All recent browsers including Safari 6+ support them.

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);

    // returns a URL you can use as a href
    return textFile;
  };

Here's an example that uses this technique to save arbitrary text from a textarea.

Another thing to note about the example is that I used the download attribute on the download link. Unfortunately, Safari does not currently support it. However in browsers that do, the file will automatically be downloaded when clicked instead of opening the file in the browser. Also, since I set the download attribute to info.txt the file will be downloaded with that name instead of the random name generated by createObjectURL.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
7

In JavaScript, you can use the following line to ask the user for saving a text file:

window.open("data:text/json;charset=utf-8," + escape("Ur String Object goes here"));

I tested this in some popular browsers some time back but please check if it works in Safari or not.

Amit
  • 13,134
  • 17
  • 77
  • 148
  • It has been 3 years since this answer was posted, but this code didn't work properly in Google Chrome. – hexicle Nov 06 '14 at 00:47
1

You can use following function to create and download a txt file:

const initDownload = (function () {
  const a = document.createElement('a')
  document.body.appendChild(a)
  // @ts-ignore
  a.style = 'display: none'
  return function (data: string, fileName: string, fileHeader) {
    a.href = fileHeader + data //Image Base64 Goes here
    a.download = fileName //File name Here
    a.click() //Downloaded file
  }
}())

initDownload(
  'content of file',
  'filename.txt',
  'data:text/json;charset=utf-8,'
)
Rashomon
  • 5,962
  • 4
  • 29
  • 67
-4

but my requirement is to generate a text file using javascript for a safari browser

That's not possible with vanilla JavaScript due to security restrictions. You can however use server-side JavaScript such as Node.JS or Ajax or some other server-side technology.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sarfraz
  • 377,238
  • 77
  • 533
  • 578