Navigate back home
GalaSoft Laurent Bugnion
CExplorer: Save Text Files with JavaScript
Many people assume that client-side JavaScript cannot perform file-system operations on the client computer. Actually, the objects needed are often found in the browsers, and the rest is only a matter of compatibility (the way IE saves files is very different from the way Firefox saves files...) and of permissions.
In order to perform critical file-system operations, the script must be granted privileges. There are different ways to get these privileges, and unfortunately, these are very different depending on the browser your application run on.
I won't debate long on the subject of security and permissions in the browser, others did that much better than me (check Google for more information). The demo here demonstrates as a proof of concept that saving a file locally is possible in both major web clients available today (IE and Firefox, tested only on Windows platform).
A few things before you start
  • In the http protocol (i.e. without saving it locally), this demo will most probably fail in Firefox. In IE, however, it may work, depending on the permissions you set for the Internet zone.
  • This demo is a proof of concept only.
    I usually use these methods either in HTA applications (running locally, granted extended privileges by IE), or in the "file:" protocol. This is what they have been designed for.
  • The code has been tested on IE6 on Windows XP and in Firefox 1.5.0.4 on Windows XP.
  • The code for Netscape 4.7 is provided in the file cexplorer.extracts.txt for reference and for historical reasons. However, the use of keywords like "try", "catch", "throw" make this demo fail in Netscape 4. This is why the code has been commented out of the demo.
  • This demo allows only saving text files, but it's a limitation I added myself in the JavaScript code. I accept no responsibility whatsoever for changes you might apply to this code and which might harm your computer.
What would you like to do?
  • Text file See the source code as text file
  • Zip file Download the demo in a Zip file
    After download, extract the content to a local folder, and open the file index.html in your browser. This starts the demo in "file" protocol, granting it extended rights.
  • Execute the demo
Execute the demo  (...)


 Overwrite file silently
About the "gslb" namespace  (...)
All my JavaScript objects are placed into a "namespace", which in fact is another JavaScript object named "gslb" (for GalaSoft Laurent Bugnion).
The purpose of such a "namespace" is to avoid polluting the global object with too many names, and to avoid name collisions.
On the downside, it is necessary to reference each call to the object with the full name, for example:
oExplorer = new gslb.CExplorer();