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