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

A

Alex

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.enablePrivilege('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.nsIFileOutputStream);
outputStream.init(file,0x20 | 0x02,00004,null);
outputStream.write(text, text.length);
outputStream.flush();
outputStream.close();
}

function read(myfile) {
try
{netscape.security.PrivilegeManager.enablePrivilege("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>
 
R

Randy Webb

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.enablePrivilege('UniversalXPConnect');}
catch (e) {alert("Permission to write file denied."); return 0;}

And now, you have not written to my hard drive. Try again :)
 
T

Thomas 'PointedEars' Lahn

Alex said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top