我一直在Firefox 17. *到51. *。的引导特权代码中使用new File(file.path);
。
file
是nsIFile
。
从Firefox 52开始,它现在出错:TypeError: Not enough arguments to File.
File and Directory Entries API已更新,包含更改 在latest spec中(有关详细信息,请参阅bug 1284987。)
现在用于Firefox 52的正确代码示例是什么。* +?
根据要求更新:
// note: aFileURL is a local file
let aFileURL = 'file:///C:/Users/***/icon.png'; // just an example
let file = Services.io.newURI(aFileURL, null, null)
.QueryInterface(Components.interfaces.nsIFileURL).file; // convert URL to nsIFile
file = new File(file.path); // Firefox 52: TypeError: Not enough arguments to File.
答案 0 :(得分:0)
感谢 Makyen
参考:Using the DOM File API in chrome code
MDN声明:var file = File.createFromFileName("path/to/some/file");
以下代码无效:(我的误解)
// Using text URL
Components.utils.importGlobalProperties(['File']);
let aFileURL = 'file:///C:/Users/***/icon.png'; // just an example
let file = File.createFromFileName(aFileURL);
// "File error: Unrecognized path" nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)"
以下代码有效:
Components.utils.importGlobalProperties(['File']);
let aFileURL = 'file:///C:/Users/***/icon.png'; // just an example
let file = Services.io.newURI(aFileURL, null, null)
.QueryInterface(Components.interfaces.nsIFileURL).file; // convert URL to nsIFile
file = File.createFromFileName(file.path);
以下代码也有效:
Components.utils.importGlobalProperties(['File']);
let aFileURL = 'file:///C:/Users/***/icon.png'; // just an example
let file = Services.io.newURI(aFileURL, null, null)
.QueryInterface(Components.interfaces.nsIFileURL).file; // convert URL to nsIFile
file = File.createFromNsIFile(file);
供参考的其他信息:
使用file = new File([], file.path);
会产生以下结果:
File { name: "C:\Users\...\icon.png", lastModified: 1487509240391, lastModifiedDate: Date 2017-02-19T13:00:40.391Z, webkitRelativePath: "", mozFullPath: "", size: 0, type: "" }
但是,使用file = File.createFromFileName(file.path);
会产生以下结果:
File { name: "icon.png", lastModified: 1403974172431, lastModifiedDate: Date 2014-06-28T16:49:32.431Z, webkitRelativePath: "", mozFullPath: "", size: 4294, type: "image/png" }
使用file = File.createFromNsIFile(file);
会产生以下结果:
File { name: "icon.png", lastModified: 1403974172431, lastModifiedDate: Date 2014-06-28T16:49:32.431Z, webkitRelativePath: "", mozFullPath: "C:\Users\...\icon.png", size: 4294, type: "image/png" }
将file
从第一个代码传递到FileReader()
会产生错误的结果。 "data:application/octet-stream;base64,"
从{2}传递file
FileReader()
的第3个代码生成正确的结果。
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAQjUlEQVR42uWbB3QU17nHv7uzq60S6sJGotniARGYUCQcZAvkhh0QosZgXPJsHwgJpiTB4Aq4JrafwTHvkZeYBBt4EELAGAwGhABjwEYgcMExEk1CjSIJaXuZyXdHO8v..."