本文介绍了如何使用Firefox插件从硬盘读取/写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能开发一个Firefox插件,可以从硬盘读取/写入文件?我应该使用什么样的代码?

解决方案

这只是链接@Hypnos和@ephemient中的代码的复制(和组合) / p>

  const {Cc,Ci} = require(chrome); 

//为文件
创建正确的路径var theFile ='d:\\q.txt';
//创建文件写入组件
var file = Cc [@ mozilla.org/file/local;1\"].createInstance(Ci.nsILocalFile);
file.initWithPath(theFile);
if(file.exists()== false)//检查文件是否存在
{
file.create(Ci.nsIFile.NORMAL_FILE_TYPE,420);
}
var foStream = Cc [@ mozilla.org/network/file-output-stream;1\"].createInstance(Ci.nsIFileOutputStream);
//使用0x02 | 0x10打开文件进行追加。
//foStream.init(文件,0x02 | 0x10,0666,0);
foStream.init(文件,0x02 | 0x08 | 0x20,0666,0);

//如果你确定在数据中永远不会有任何非ASCII文本,你可以
//直接调用foStream.write(data,data.length)
var converter = Cc [@ mozilla.org/intl/converter-output-stream;1\"].createInstance(Ci.nsIConverterOutputStream);
converter.init(foStream,UTF-8,0,0);
converter.writeString('这是死亡,你是谁?');
converter.close(); //关闭foStream


Is it possible to develop a Firefox addon that can read/write a file from hard disk? What code should I use?

解决方案

it's just copy (and combination ) of code in links @Hypnos and @ephemient provided:

const {Cc,Ci} = require("chrome");

//create proper path for file
var theFile = 'd:\\q.txt';
//create component for file writing
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath( theFile );
if(file.exists() == false) //check to see if file exists
{
    file.create( Ci.nsIFile.NORMAL_FILE_TYPE, 420);
}
var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
//foStream.init(file, 0x02 | 0x10, 0666, 0);
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);

// if you are sure there will never ever be any non-ascii text in data you can
// also call foStream.write(data, data.length) directly
var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
converter.init(foStream,"UTF-8", 0, 0);
converter.writeString('Hi, This is Death. Who are you?');
converter.close(); // this closes foStream

这篇关于如何使用Firefox插件从硬盘读取/写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 13:52