By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,052 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,052 IT Pros & Developers. It's quick & easy.

Can I write/read local file on hard drive in FireFox (Netscape) ?

P: n/a
Yes you can:

<html><head><script language="javascript">

SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
alert(read('C:\\temp\\test.txt'));

function SaveToFile (text, fileName) {
try
{netscape.security.PrivilegeManager.enablePrivileg e('UniversalXPConnect');}
catch (e) {alert("Permission to write file denied."); return 0;}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile );
file.initWithPath(fileName);
if (!file.exists()) file.create(0x00, 0644);
var outputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
outputStream.init(file,0x20 | 0x02,00004,null);
outputStream.write(text, text.length);
outputStream.flush();
outputStream.close();
}

function read(myfile) {
try
{netscape.security.PrivilegeManager.enablePrivileg e("UniversalXPConnect");}
catch (e) {alert("Permission to read file denied."); return '';}
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile );
file.initWithPath(myfile);
if (!file.exists()) {alert("File not found."); return '';}
var is =
Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(
Components.interfaces.nsIFileInputStream );
is.init(file,0x01, 00004, null);
var sis =
Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(
Components.interfaces.nsIScriptableInputStream );
sis.init(is);
var output = sis.read(sis.available());
return output;
}

</script></head><body></body></html>

Mar 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alex said the following on 2/28/2006 11:03 PM:
Yes you can:
Yes I can what?

No DTD.
<html><head><script language="javascript">
Type attribute is mandatory, language attribute is deprecated.
SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
alert(read('C:\\temp\\test.txt'));

function SaveToFile (text, fileName) {
try
{netscape.security.PrivilegeManager.enablePrivileg e('UniversalXPConnect');}
catch (e) {alert("Permission to write file denied."); return 0;}


And now, you have not written to my hard drive. Try again :)
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 1 '06 #2

P: n/a
Alex wrote:
Yes you can:


Nobody ever doubted this. In fact, this has been mentioned at least by me
several times before. However, the script needs the privileges, and it
can only get the privileges if it is signed, or, if it is not signed, the
security settings of the client are reduced, which opens a security leak
for all other Web sites.
PointedEars
Mar 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.