Re: Reading/Writing from files
jef peeraer <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.xpfe |
|---|---|
| Message-ID | <[email protected]> |
On 02/22/2012 04:17 PM, Shibu Narayanan wrote:
> Hi,
> I see the below sample code given
>
> Read into a stream or a string
>
> This will allow you to read a file without locking up the UI thread. This sample code uses NetUtil.jsm. You can pass an nsIFile object directly into NetUtil.asyncFetch:
>
> <---- code starts here ----->
> Components.utils.import("resource://gre/modules/NetUtil.jsm");
>
> NetUtil.asyncFetch(file, function(inputStream, status) {
> if (!Components.isSuccessCode(status)) {
> // Handle error!
> return;
> }
>
> // The file data is contained within inputStream.
> // You can read it into a string with
> var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
> alert(data);
> });
>
> <---- code ends here ----->
>
> I do not understand the code.
> Where do I put the NetUtil.jsm file? Do I create a folder by name<base_dir>/gre/modules/ ? Where do I specify the data file name in the code?
> Putting this code in some event handler will show the contents of a file?
>
> Can someone post a full method/function code which accepts a file URL and prints out the contents? I have already downloaded NetUtil.jsm and FileUtil.jsm and added to my project.
>
> -Shibu
you have to create a 'modules' directory in you're project, and define a
resource entry in the chrome.manifest, which points to that new
directory, eg example of my chrome manifest
resource surveys file:////home/jpe/proj/surveys/cksdos/modules/
or
resource surveys ../modules/
jef