Javascript 如何将本地URI转换为路径?
如何将本地(文件系统)URI转换为路径?Javascript 如何将本地URI转换为路径?,javascript,firefox-addon,firefox-addon-restartless,Javascript,Firefox Addon,Firefox Addon Restartless,如何将本地(文件系统)URI转换为路径? 可以使用nsioservice+newURI()+QueryInterface(Components.interfaces.nsIFileURL)+file.path完成,但这似乎是一条漫长的路。 有较短的路吗 下面是一个示例代码: var aFileURL = 'file:///C:/path-to-local-file/root.png'; var ios = Components.classes["@mozilla.org/network/io-s
可以使用
nsioservice
+newURI()
+QueryInterface(Components.interfaces.nsIFileURL)
+file.path
完成,但这似乎是一条漫长的路。有较短的路吗 下面是一个示例代码:
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var url = ios.newURI(aFileURL, null, null); // url is a nsIURI
// file is a nsIFile
var file = url.QueryInterface(Components.interfaces.nsIFileURL).file;
console.log(file.path); // "C:\path-to-local-file\root.png"
支持的方式实际上是您已经在做的事情。如果您觉得它太冗长,请自己编写一个helper函数。当然,您可以使用各种辅助工具将其缩短一点
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/Services.jsm");
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var path = Services.io.newURI(aFileURL, null, null).
QueryInterface(Ci.nsIFileURL).file.path;
或:
你能举个例子说明你从什么开始,到什么结束吗。可选:还有一个片段来显示您当前的操作方式。啊,很酷的问题,您正在尝试将其转换为一个可以为API提供信息的路径,对吗?@Noitidart我想根据我的其他两个问题将其用于文件上载请注意:
var path=Services.io.newURI(aFileURL,null,null)。QueryInterface(Ci.nsileurl)。file.path代码>导致错误(因为文件不存在)。。。它应该是:var file=Services.io.newURI(aFileURL,null,null).QueryInterface(Ci.nsileurl).file.path代码>。。。文件
的值现在是路径
。下一个也一样;)
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/NetUtil.jsm");
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var path = NetUtil.newURI(aFileURL).QueryInterface(Ci.nsIFileURL).file.path;