Firefox 52 "New File" changes and error

2.2k views Asked by At

I have been using new File(file.path); in bootstrapped privileged code from Firefox 17.* to 51.*.
file is an nsIFile.

As of Firefox 52, it now gives an error: TypeError: Not enough arguments to File.

ref: Firefox 52 for developers

The File and Directory Entries API has been updated to include changes in the latest spec (see bug 1284987 for the exact details).

What would be an example of the proper code to use now for Firefox 52.*+?

Update upon request:

// 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.
1

There are 1 answers

6
erosman On

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..."