DevelopmentOpen SourceProgrammingXRap

How-to: Read Files in XUL

By November 25, 2008August 21st, 20133 Comments

After some weeks of procrastination and going nuts over other projects, I’m getting back to XUL and my XRap program.  I’ve been asked to change my current XRap code (currently the 0.1 is in C#) to XUL, which would increase its portability, and it just makes sense to make a XULRunner Packager in XUL.  Unfortunately, I must undertake the task of learning how to program in XUL and JavaScript.  So I’ve managed to complete most of my other programming courses, so now I can focus on this.  One of my main problems was that I had no idea of how the JavaScript file was linked to the XUL file, and how it would know where to pass output, or where to get output.  My second major problem was that I couldn’t find any help on how to access the file system using JavaScript.

After a quick chat on IRC, a colleague directed me to the File I/O site on the Mozilla Developer Network.  I found it to be only somewhat useful, but even more so when you take a look at the ‘Read local files and write local files’ page at Captain’s Universe.  Combining the two I’ve managed to come up with this function in my .js file:

function getFile()
{
var dup = document.getElementById(‘tb_input’);

var file = Components.classes[“@mozilla.org/file/local;1”]                .createInstance(Components.interfaces.nsILocalFile);

var fstream = Components.classes[“@mozilla.org/network/file-input-stream;1”] .createInstance(Components.interfaces.nsIFileInputStream);
var sstream = Components.classes[“@mozilla.org/scriptableinputstream;1”] .createInstance(Components.interfaces.nsIScriptableInputStream);

file.initWithPath(filePath);
if ( file.exists() == false ) {
dup.value = “File does not exist”;
}

fstream.init(file, 0x01, 00004, null);
sstream.init(fstream);

var output = sstream.read(sstream.available());
dup.value = output;
sstream.close();
fstream.close();
}

file – gets the nsILocalFile interface component, making file of type nsILocalFile
filepath – use the absolute path, and the double backslash to avoid the escape character.
file – gets the nsILocalFile interface component, making file of type nsILocalFile inherited from nsIFile

  • initWithPath(filepath) – takes in the file path, and initializes it
  • exists() – checks if there is a valid file associated with file

fstream – gets the nsIFileInputStream Interface component, making fStream of type nsIFileInputStream

  • init(file, file_io_flags,file_mode_flags, behavior_flags) –
OptionType
filensIFile
File_io_flags0x01Read only
 0x02Write only
 

0x04

Read and Write
 0x08Create File
 0x10Append
 0x20Truncate
 0x40Sync
 0x80Exclude
   
File_mode_flags00400Read by owner
 00200Write by owner
 00100Execute by owner
 00040Read by Group
 00020Write by Group
 00010Execute by Group
 00004Read by Others
 00002Write by Others
 00001Execute by Others
* Mode flags can by added to combine flag
* File_mode_flags is only applicable to UNIX based systems
 
Behavour_flags0Default

sstream – gets the nsIScriptableInputStream interface component, making sStream of type nsIScriptableInput Stream

  • init(nsIFileInputStream) – initializes the file input stream
  • read(int32 count) – reads the count bytes in the stream
  • available() – returns the number of bytes in the stream

Well, it actually didn’t give me as much of a headache that I thought it might have.  It is just a matter of thinking like a Mozilla/JavaScript person, and knowing what the interfaces can do.  Next up Writing Files…

3 Comments

  • Assuming you’re in a xulrunner application or a firefox add-on, you will not need to use permission code like this:
    netscape.security.PrivilegeManager.enablePrivilege(”UniversalXPConnect”);

    You already have full chrome privileges.

  • Jeff Singer says:

    Hi

    I am trying to use the above code for reading in a PNG file, and then displaying on my web page..

    Since I am a newbie… I would appreciate any pointers you have for me.

    Thanks

  • noerone says:

    can you bring me sample project read & write files in XUL
    sorry i’m newbie…. 🙂

Leave a Reply