Skip to content

Instantly share code, notes, and snippets.

@darkowlzz
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save darkowlzz/0e597d4ef4e930470106 to your computer and use it in GitHub Desktop.

Select an option

Save darkowlzz/0e597d4ef4e930470106 to your computer and use it in GitHub Desktop.
synchronous Read using nsIChannel
const { Cu, Ci } = require('chrome');
const { NetUtil } = Cu.import('resource://gre/modules/NetUtil.jsm');
let charset = 'UTF-8';
let channel = NetUtil.newChannel('chrome://foo/content/test-file.txt', charset, null);
// `channel` obtained is an nsIChannel object. Open the channel synchronously.
let stream = channel.open();
// Get the stream length
let count = stream.available();
// Read the input stream and convert to string.
let data = NetUtil.readInputStreamToString(stream, count, { charset: charset });
// Close the input stream
stream.close();
console.log(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment