15

I am a novice-intermediate programmer taking a stab at AJAX. While reading up on JavaScript I found it curious that most of the examples I've been drawing on use PHP for such an operation. I know many of you may argue that 'I'm doing it wrong' or 'JavaScript is a client-side language' etc. but the question stands. . .can you write a file in only JavaScript?

9 Answers 9

20

Yes, of course you can. It just depends on what API objects your javascript engine makes available to you.

However, odds are the javascript engine you're thinking about does not provide this capability. Definitely none of the major web browsers will allow it.

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

2 Comments

Some browsers do allow it, if they support the File System Access API.
@AndersonGreen I don't think that existed in 2009, but this is a good addition to the question. Maybe provide a new answer.
16

You can write cookies with Javascript, on newer browsers you also have an SQLite database to store client side data. You cannot store data in an arbitrary location on the disk though.

Comments

6

You can use something like Google Gears to produce JS applications which are capable of storing data in a local cache or database. You can't read or write arbitrary areas of the disk though. (This was written in 2009 - Google Gears is now deprecated)

These days, you should be looking at the local storage capabilities provided by HTML5

Comments

4

No. You could use JavaScript to create an AJAX request to a server-side processing script, but allowing JS to directly write to disk - either client-side or server-side - would be a massive, nasty, glaring, unforgivable browser security hole.

Comments

2

The short answer is no; you cannot by default write a file to the local disk, by using plain JavaScript in a browser. You'll need a helper to do that. For example, TiddlyWiki is a wiki engine that is just a single, static HTML file, but it can write itself to disk with the help of a Java applet (Tiddly Saver).

Comments

1

You can in Windows Scripting Host.

Comments

1

Next version of chrome (v52) made this possible with fetch api + service worker + streams, you can enable streams now with a flag...

you can go to the StreamSaver.js to see some examples of how to use it.

You can do something like this:

const writeStream = fs.createWriteStream('filename.txt')
const encoder = new TextEncoder
let data = 'a'.repeat(1024)
let uint8array = encoder.encode(data + "\n\n")

writeStream.write(uint8array)
writeStream.close()

Or just go ahead and look at the demos: https://jimmywarting.github.io/StreamSaver.js/example.html

Comments

1

If you just need to let user download a file (.txt, .csv, images and others) via browser download dialog, you can use data URIs with <a href=... download=.../> tag.

For example (for text file):

<a href="data:text/plain;charset=utf-8,TEXT_HERE" download="filename.txt"> Click to download </a>

You can also set the attribute href and download using javascript, and use element.click() to trigger the download.

However, this method cannot write a file without user confirming the file download dialog.

Comments

0

Nope, Javascript is not allowed to access the filesystem at all, its a security restriction in the browser. The only way you can really do it is with ActiveX, but then your limiting yourself to using IE.

Edit: AS the above post states, it could be possible if your engine allowed it, however I don't know of one browser engine (which is what I asusme you are writing it for) that will allow you to.

3 Comments

ActiveX is not the only way. You can use Java without limiting yourself to just IE.
We are talking about Javascript here, not Java, and the most common ways to do what he asks is with activeX, whic limits you to IE
How is ActiveX more "JavaScript" than Java? You're simply embedding an additional object inside your HTML file and letting JavaScript use it as a helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.