How to create a file using javascript in Mozilla Firefox

12,360

Solution 1

You can write files in JavaScript in Firefox, but you have to use an XPCOM object (internal browser API). This is not allowed for JavaScript loaded from a web page, and it is intended to be used by JavaScript running inside a Firefox add-on (with high level of privileges).

There is a way for unprivileged (web page) JavaScript to request more privileges and if the user grants it (there will be a pop up dialog asking for permission), the web page code would be able to write to a file.

But before you read further, a warning:

This is not standard JavaScript and I would not recommend this approach unless you are developing a very specific application, that will be used in a very specific way (like for example, http://www.tiddlywiki.com/ a client-side JavaScript-HTML only wiki).

Requesting XPCOM privileges on a website is a bad practice! It's basicly equivalent to running an .exe you just downloaded from a site. You are asking a user to grant full access to their computer (read, write, execute) with the identity of the user running Firefox.

Request permission to use XPCOM (this will prompt the user for confirmation, no way to avoid it):

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

Then, write to a file using an XPCOM object (example code from Mozilla Developer Network):

   1. // file is nsIFile, data is a string  
   2. var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].  
   3.                          createInstance(Components.interfaces.nsIFileOutputStream);  
   4.   
   5. // use 0x02 | 0x10 to open file for appending.  
   6. foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);   
   7. // write, create, truncate  
   8. // In a c file operation, we have no need to set file mode with or operation,  
   9. // directly using "r" or "w" usually.  
  10.   
  11. // if you are sure there will never ever be any non-ascii text in data you can   
  12. // also call foStream.writeData directly  
  13. var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].  
  14.                           createInstance(Components.interfaces.nsIConverterOutputStream);  
  15. converter.init(foStream, "UTF-8", 0, 0);  
  16. converter.writeString(data);  
  17. converter.close(); // this closes foStream  

You can find more information about I/O in Firefox using XPCOM here: https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O

Solution 2

Javascript from websites cannot access the local file system.

If you like to store data either store it on the server or in a cookie.

Solution 3

writing to the file system directly from a browser is prohibited for security reasons. With html5 however it'll be possible to have offline storage support. Take a look here.

Grz, Kris.

Solution 4

Javascript executes in a client-side context.

http://www.tek-tips.com/viewthread.cfm?qid=1171273&page=1

Solution 5

There will be an API for this.. File Writer API. The early specification is here: http://www.w3.org/TR/file-writer-api/ It is not implemented in any browser yet.

Update: It seems there already exists an implementation. Check out http://caniuse.com/filesystem and http://www.html5rocks.com/en/tutorials/file/filesystem/

Share:
12,360

Related videos on Youtube

minu
Author by

minu

Updated on April 25, 2022

Comments

  • minu
    minu about 2 years

    I want to write a function in javascript which creates a file and write some content to it, iam working with firefox, can anybody help me in this case.

    Thanks...

  • minu
    minu almost 14 years
    But it is showing some error like "Create object is not defined".