File IO

From PlainOldWebserver

Jump to: navigation, search

File I/O is one of the big reasons to write SJS or an extension. You have access to most of the file system. If you are writing an extension, create a sub-directory for your extension in the Profile directory. This should lessen the chances of a collision with another extension. Here is code for saving a file.

Writing to the Profile Directory

 <?sjs
   try {
     var filename = "new_file.txt";
     var text = "This is text for your new file\n";
     var dirservice = Components.classes["@mozilla.org/file/directory_service;1"].
         getService(Components.interfaces.nsIProperties);
     var f = dirservice.get('ProfD', Components.interfaces.nsILocalFile);
     f = pow_append_path(f, filename);
     var outputStream =
         Components.classes["@mozilla.org/network/file-output-stream;1"].
         createInstance( Components.interfaces.nsIFileOutputStream );
     outputStream.init( f, 0x04 | 0x08 | 0x20, 420, 0 );
     var result = outputStream.write( text, text.length );
     outputStream.close();
   } catch(e) {
     alert(e);
   }
 ?>

Result in YOUR-PROFILE-DIR/new_file.txt:

 This is text for your new file

Reading from the Profile Directory

  <?sjs
  function xpcomFileRead(filePath) {
    try {
       var dirservice = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties);
       var file = dirservice.get('ProfD', Components.interfaces.nsILocalFile);
       file = pow_append_path(file, filePath);
       var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].
       createInstance(Components.interfaces.nsIFileInputStream);
       inputStream.init(file, -1, 0,0);
       var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].
       createInstance(Components.interfaces.nsIScriptableInputStream);
       sInputStream.init(inputStream);
       return sInputStream.read(sInputStream.available());
     } catch(e) {
       document.write(e);
       return false;
     }
     return false;
  }
  var f = "bookmarks.html";
  contents = xpcomFileRead(f);
  document.write(contents);
  ?>

Full File Access on Windows

pow_file seems to only be able to read files inside the htdocs directory. To read files anywhere on the users file system you can use the following xpcom function:

function xpcomFileRead(filePath) { 
  try {
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(filePath);
    if (!file.exists()) {
       document.write("//Error File Does Not Exist");
       return false;
    }
    var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
    inputStream.init(file, -1, 0,0);
    var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
    sInputStream.init(inputStream);
    return sInputStream.read(sInputStream.available());
   } catch(e) {
     document.write(e);
     return false;
   }
   return false;
}
var f = "c:\\temp\\test.txt";
contents = xpcomFileRead(f);
document.write(contents);

On windows the write file also had problems. The solution was again to use xpcom code:

<?sjs
filename = "C:\\deleteme.txt";
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filename);
data = "delete me please";
// file is nsIFile, data is a string
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                       .createInstance(Components.interfaces.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
foStream.write(data, data.length);
foStream.close();
Personal tools
Navigation