473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
2 9529
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Folks This is probably starring me in the face but how do I read the contents of a hard drive directory? I've been playing around with the opendir($dir) sample from php.net and can read from...
6
by: chuck clark | last post by:
Hi, I understand that for security and other reasons that javascript does not support writing/reading to text files. I am looking for a way that will however. This is what i am trying to do. I...
20
by: Steve | last post by:
I have a web app that needs to parse through a file that is located on the client machine. I get the file string from a query string & then parse it. It is working fine on my development box but...
2
by: Trygve Lorentzen | last post by:
Hi, I want to read a file from a http request (say, http://www.foo.com/test.exe) to my local hard drive. How can I do that? I need specific code examples here, not just "use HttpRequest and get...
23
by: wylbur37 | last post by:
I'm running an Apache server on my own computer (Windows XP Pro). I wrote a simple PHP script (called test3.php) that I'm running by putting the following URL in the address bar of the browser...
1
by: demareg | last post by:
When run from local files, my javascript code sets cookies properly and they are visible under the Firefox options. Unfortunately, document.cookie returns an empty string as if cookies are not...
2
by: Wanjun Yu | last post by:
What is the class name for open/write/read a text file? I can't find it in the reference book. Thanks! WJ
4
bluegreenisland
by: bluegreenisland | last post by:
Hi everyone, I am new to this, so am bound to make blunders ~just let me know & I'll correct them. I am hoping to learn how to write to a file, make changes to the data, and read. I have a...
3
BishoMicho
by: BishoMicho | last post by:
What i want to do is to implement visitor counter in the home page of a web application, with daily visitors and total visitors. i don't want to use code behind, i want to use javascript to write...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.