How-to / Open Source / Programming

How-to: Read Files in XUL

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[“;1”]

var fstream = Components.classes[“;1”]
var sstream = Components.classes[“;1”]

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

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

var output =;
dup.value = output;

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) –
Option Type
file nsIFile
File_io_flags 0x01 Read only
  0x02 Write only


Read and Write
  0x08 Create File
  0x10 Append
  0x20 Truncate
  0x40 Sync
  0x80 Exclude
File_mode_flags 00400 Read by owner
  00200 Write by owner
  00100 Execute by owner
  00040 Read by Group
  00020 Write by Group
  00010 Execute by Group
  00004 Read by Others
  00002 Write by Others
  00001 Execute by Others
* Mode flags can by added to combine flag
* File_mode_flags is only applicable to UNIX based systems
Behavour_flags 0 Default

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 thoughts on “How-to: Read Files in XUL

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

    You already have full chrome privileges.

  2. 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s