OS.File将chrome文件复制到桌面

时间:2014-09-18 07:21:49

标签: firefox-addon

我试图使用OS.File将Chrome路径复制到桌面,但它一直在抛出错误。这可能吗?

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())
  }
);

1 个答案:

答案 0 :(得分:3)

由于chrome://branding/content/icon16.png不是文件,我猜你不能。

但可以在NetUtliOS.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);
    }
  )
})