0

I'm trying to use OS.File to copy a chrome path to desktop but it keeps throwing errors. Is this possible?

var promise = OS.File.copy('chrome://branding/content/icon16.png', OS.Path.join(OS.Constants.Path.desktopDir, 'copied.png'));
promise.then(
  function(aVal) {
    console.log('suc')
  },
  function(aReason) {
    console.error('FAIL, aReason:', aReason)
    console.error('FAIL, aReason:', aReason.toString())
  }
);
Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

3

Since chrome://branding/content/icon16.png is not a file, I guess you can't.

But it is possible to split the job between NetUtli and OS.File

NetUtil.asyncFetch("chrome://branding/content/icon16.png", function(inputstream, code){
  var bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
  bis.setInputStream(inputstream);
  var data = new Uint8Array(bis.available());
  bis.readArrayBuffer(data.length, data.buffer);

  OS.File.open(OS.Path.join(OS.Constants.Path.desktopDir, 'copied.png'), {write: true, append: false, create: true}).then(
    function success(file){
      file.write(data);
      file.close();
    },
    function fail(reason){
      console.log(reason);
    }
  )
})
paa
  • 5,048
  • 1
  • 18
  • 22
  • 1
    Thanks man. I was hoping I could do it totally async with OS.File, my next option was XHR and OS.File. Would using XHR be more async then NetUtil? http://stackoverflow.com/questions/25492225/how-to-download-image-to-desktop-with-os-file – Noitidart Sep 18 '14 at 15:14