Is it possible to write to a file (on a disk) using JavaScript?

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?

Answers:

Answer

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

Answer

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.

Answer

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.

Answer

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.

Answer

You can in Windows Scripting Host.

Answer

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

Answer

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.

Answer

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

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.