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

accessing files

P: n/a
I am thinking of using javascript as a tool to be used in a local PC (quick
development).
I would like to know if there is a way to read/write files from the local HD
(eg: read, write)
if not, is there any tool that can be used ?
thank you.

--

yours -
Arie Z.

============================================
Arie Zychlinski
R&D Consulting & Development
P.O.Box 536
Kfar-Saba 44104
ISRAEL

Mobile: 972-58-320230
Phone: W: 972-9-7673074 H: 972-9-7658268

E-Mail: ar*****@bezeqint.net
===========================================
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <3f******@news.bezeqint.net>, ar*****@bezeqint.net
enlightened us with...
I am thinking of using javascript as a tool to be used in a local PC (quick
development).
I would like to know if there is a way to read/write files from the local HD
(eg: read, write)
if not, is there any tool that can be used ?


Windows Script Host (WSH) and an HTA would work for reading and writing
client-side files. Assuming you're running Windows...

--
--
~kaeli~
If a parsley farmer is sued, can they garnish his wages?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2

P: n/a
Hi,

Arie Zychlinski wrote:
I am thinking of using javascript as a tool to be used in a local PC (quick
development).
I would like to know if there is a way to read/write files from the local HD
(eg: read, write)
if not, is there any tool that can be used ?
thank you.


The problem with that is that there is no cross-compatible way to
read/write files. In other words, you must use other objects in IE than
in Mozilla.

Anyway, for IE, take a look at the FileSystemObject, it's the most
convenient way to read/write local files from a local application.

For Netscape, you can use the java.io classes, in other words use the
Java objects directly from JavaScript.

I add a method from my CExplorer class, which might help you to
understand better what I mean.

HTH,

Laurent

/**----------------------------------------------------------------------------
* Saves a text file to the specified path.
* If the file already exists, overwrites it without warning.
*
* @param [javascript.String] strFullPath The file's full path.
* @param [javascript.String] strContent The file's content.
* @param [boolean] bOverwrite If true, the file is overwritten
* without question if it exists. If false, the user must confirm
* the overwriting.
* @return [boolean] True if the file has been saved, false otherwise.
*/
CExplorer.saveTextFile = function( strFullPath, strContent, bOverwrite )
{
if ( ( navigator.appName != "Netscape" )
&& ( navigator.appName != "Microsoft Internet Explorer" ) )
{
alert( CExplorer.ALERT001 );
return;
}

if ( ( navigator.appName == "Netscape" )
&& ( parseFloat( navigator.appVersion ) >= 5 ) )
{
alert( CExplorer.ALERT001 );
return;
}

if ( navigator.platform.indexOf( "Win" ) == -1 )
{
alert( CExplorer.ALERT003 );
return;
}

if ( navigator.appName.indexOf( "Netscape" ) == -1 )
{
var fso = new ActiveXObject( "Scripting.FileSystemObject" );

if ( bOverwrite
|| !fso.FileExists( strFullPath )
|| confirm( CExplorer.ALERT002 ) )
{
var flOutput = fso.CreateTextFile( strFullPath, true );
flOutput.Write( strContent );
flOutput.Close();
return true;
}
else
{
return false;
}
}
else
{
netscape.security.PrivilegeManager.enablePrivilege (
"UniversalFileRead" );

var flCheck = new java.io.File( strFullPath );

if ( bOverwrite
|| !flCheck.exists()
|| confirm( CExplorer.ALERT002 ) )
{
netscape.security.PrivilegeManager.enablePrivilege (
"UniversalFileWrite" );
var fsOutput = new java.io.FileOutputStream( strFullPath );
var flOutput = new java.io.DataOutputStream( fsOutput );

flOutput.writeBytes( strContent );

flOutput.flush();
fsOutput.close();
return true;
}
else
{
return false;
}
}
}
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.