server-side JavaScript: Example 1: File class


G

GVDC

Example server-side JavaScript Web script, working with files on server

//File class;
//Open file, lock, write text to file, then read and print data from file
//
printf("<html><body>");

var fileobj = new File("myfile.txt"); //File class

//open for RW, truncate and if not existing create mode 0644
if ( fileobj.open("rwtc", 0644)==true ) {
//set exclusive lock
if ( fileobj.lock("x")==true ) {

//write ok
if ( fileobj.write("abc text text more text")!=(-1) ) {
//rewind to beggining
fileobj.seek(0);

//read (with no arg) whole file
var strfromfile = fileobj.read();

//data from file successfully read
if ( strfromfile!=null ) {
printf("Data read from file:<br>");
printf(strfromfile, "<br>");
}


}

}
//close file
fileobj.close();

}
//error opening file
else {
//ie. invalid path, no permission etc.
//Error open file [errno 2] No such file or directory
printf("Error open file [errno " ,Server.errno(), "] ", Server.errstr(), "<br>");
}
printf("</body></html>");





--
 
Ad

Advertisements


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

Top