Firefox コード例:ローカルファイルを指すURL(file:///)を作る
Firefoxアドオン上で、ローカルファイルを指すURL(file:///)を作るJavascript例を挙げる。
cf. nsIFileProtocolHandler
逆に、file:/// から nsIFile を取得したい場合は以下が参考になる。
cf. nsIFile とパス文字列
/***********************************************
file:/// の形式のファイルパス(URL)を作る
*/
//一度、普通のファイルを作る
var path = "c:\\Users\\xxx\\Desktop\\tmp\\";
var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
aFile.initWithPath(path);
aFile.append("hoge.txt");
//ファイルプロトコルハンドラの取得
var fph = Components.classes["@mozilla.org/network/protocol;1?name=file"]
.getService(Components.interfaces.nsIFileProtocolHandler);
//普通のファイルパスを file:/// 形式に変換する
var fileURL = fph.getURLSpecFromFile(aFile);
alert("拡張機能のファイルURL=" + fileURL);
cf. nsIFileProtocolHandler
逆に、file:/// から nsIFile を取得したい場合は以下が参考になる。
cf. nsIFile とパス文字列
スポンサーサイト