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. Commented Apr 2, 2013 at 0:33
  • Can you give more information about the problem you're trying to solve? Commented 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. Commented 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. Commented 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? Commented 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

Sign up to request clarification or add additional context in comments.

7 Comments

i have used the second link in IE . but no result. Is it need any special plug in ?
i used it in my site ( linux server ) .
You need ActiveX plugin installed.
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 ?
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.
|
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 Comment

it just download a file with part extension. but i need to create a .txt file. possible ?
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 Comments

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.
Right, I forgot about the file api. That solves reading local files on modern browsers, but not writing.
+1 good point. But it's possible to open a text file using XMLHttpRequest on local filesystem.
@metal_fan Is it? I don't think so.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.