Thanks to Makyen
ref: Using the DOM File API in chrome code
MDN stated: var file = File.createFromFileName("path/to/some/file");
The following code didn't work: (my misunderstanding)
// 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)"
The following code works:
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);
The following code also works:
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);
Additional info for reference:
Using file = new File([], file.path);
produces the following:
File { name: "C:\Users\...\icon.png", lastModified: 1487509240391, lastModifiedDate: Date 2017-02-19T13:00:40.391Z, webkitRelativePath: "", mozFullPath: "", size: 0, type: "" }
However, using file = File.createFromFileName(file.path);
produces the following:
File { name: "icon.png", lastModified: 1403974172431, lastModifiedDate: Date 2014-06-28T16:49:32.431Z, webkitRelativePath: "", mozFullPath: "", size: 4294, type: "image/png" }
Using file = File.createFromNsIFile(file);
produces the following:
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" }
Passing the file
from the first code to FileReader()
produces the wrong result. "data:application/octet-stream;base64,"
Passing the file
from the 2nd & 3rd code to FileReader()
produces the correct result.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAQjUlEQVR42uWbB3QU17nHv7uzq60S6sJGotniARGYUCQcZAvkhh0QosZgXPJsHwgJpiTB4Aq4JrafwTHvkZeYBBt4EELAGAwGhABjwEYgcMExEk1CjSIJaXuZyXdHO8v..."