0

Small question:

Is it possible to make new file using JavaScript? I'm looking for something like the PHP function fopen and fput. Is this totally impossible? And what about JavaScript function OpenTextFile()?

6
  • @IswantoSan That is not a dupe, both the Q and A imply IE and ActiveX.
    – bfavaretto
    Apr 2, 2013 at 0:33
  • Can you give more information about the problem you're trying to solve?
    – jwrush
    Apr 2, 2013 at 0:35
  • @jwrush , actually i need to put some data in a new txt file. so i need to create first and then put data. Apr 2, 2013 at 1:02
  • That can't be one from the client side. Send the data to the server, then serve a new txt file for download.
    – bfavaretto
    Apr 2, 2013 at 1:08
  • Is the text file just for storing data you're going to use in the javascript application later, or do you need to use it otherwise like a text file, i.e., in other applications?
    – jwrush
    Apr 2, 2013 at 1:11

3 Answers 3

2

Only possible with HTML5 apparently.

http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=exNerx1ZaKk

And here's creating text files:

create a text file using javascript

7
  • i have used the second link in IE . but no result. Is it need any special plug in ? Apr 2, 2013 at 0:58
  • i used it in my site ( linux server ) . Apr 2, 2013 at 1:00
  • You need ActiveX plugin installed.
    – Eugene
    Apr 2, 2013 at 1:07
  • will you please make me clear . This plugin is for IE . ok . But is it work in linux server ? I mean i wanna use this file in my site hosted on linux server. possible ? Apr 2, 2013 at 1:18
  • I did not write the solution in the link, merely found the link for you. I don't see why it wouldn't work on a linux server. The only thing I can think is that it may not work on the latest versions of IE as ActiveX was dropped. Give me 5 mins and I'll look a bit further into it.
    – Eugene
    Apr 2, 2013 at 1:31
1

Here are some resources to get you started.

Writing to a file/directory: http://www.html5rocks.com/en/tutorials/file/filesystem/

Downloading a file: http://html5-demos.appspot.com/static/a.download.html

Downloading a zip: http://stuk.github.com/jszip/

Reading a file: http://www.html5rocks.com/en/tutorials/file/dndfiles/

The top 2 only work in chrome.

jszip has mixed support, and reading files with HTML5 has support in all modern browsers.

It is possible to set the data to a link's href. But a user will have to right click > save link as and add the extension.

<a id="test" target="_blank">click me</a>

document.getElementById('test').href = "data:text/json;charset=utf-8," + escape("Your String goes here");
1
  • it just download a file with part extension. but i need to create a .txt file. possible ? Apr 2, 2013 at 1:32
0

In the browser, no. Server-side (as in node.js), yes.

There is no js function to open a text file from the file system, but you can fetch the contents of a text file on your server with an Ajax request.

9
  • w3.org/TR/FileAPI
    – VeXii
    Apr 2, 2013 at 0:42
  • I suspect the File API won't do what slash-bang is looking for, as I suspect he wants to do general file IO in pure js, so bfavaretto is right in spirit.
    – jwrush
    Apr 2, 2013 at 0:47
  • 1
    Right, I forgot about the file api. That solves reading local files on modern browsers, but not writing.
    – bfavaretto
    Apr 2, 2013 at 0:47
  • +1 good point. But it's possible to open a text file using XMLHttpRequest on local filesystem.
    – Yang
    Apr 2, 2013 at 0:51
  • @metal_fan Is it? I don't think so.
    – bfavaretto
    Apr 2, 2013 at 0:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.