How to create a file using javascript in Mozilla Firefox
Asked Answered
L

8

8

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

Linsk answered 18/6, 2010 at 7:10 Comment(1)
I answered the question considering it refers to creating a file in the client-side, the same way TiddlyWiki does. Some people have dismissed the question as they interpret you are asking if JavaScript can create files in the server side. Maybe you should clarify.Leghorn
L
4

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

Leghorn answered 18/6, 2010 at 20:58 Comment(0)
S
3

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.

Sybilsybila answered 18/6, 2010 at 7:16 Comment(0)
D
2

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.

Derringdo answered 18/6, 2010 at 7:16 Comment(0)
A
1

Javascript executes in a client-side context.

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

Ambros answered 18/6, 2010 at 7:12 Comment(1)
But it is showing some error like "Create object is not defined".Linsk
T
1

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/

Those answered 21/6, 2010 at 14:19 Comment(0)
I
0

While everyone who's responded that javascript does not have the ability to write files on a remote server are correct, and this is true for security reasons, what you want to accomplish may still be possible.

For example, if you wanted to make it possible to create a file on your website with the use of javascript, you can do so with some server side scripting language and and AJAX call.

Example:

You have a file on your server called update_last_access.php which will create a file which stores the last time the file was accessed in some arbitrary file.

If you then had your javascript function make an AJAX call out to that script, for instance, in jquery

$.get("update_last_access.php")

Then this would execute the server side script and write to the file.

Before any more help can be provided for you, you're going to have to clarify what you're trying to do.

Invalid answered 18/6, 2010 at 7:28 Comment(0)
I
0

You can read files from the filesystem in JavaScript with Firefox 3.6 - see my EPUB reader proof of concept, for example.

You can't write files directly from JavaScript, though. You have to go via a server.

Interconnect answered 18/6, 2010 at 7:31 Comment(0)
T
0

Mozilla is planning to include FileSaver to Gecko 9: https://bugzilla.mozilla.org/show_bug.cgi?id=557540

Tippett answered 30/10, 2011 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.