mozilla

Revision 68580 of Using the DOM File API in chrome code

  • Revision slug: Extensions/Using_the_DOM_File_API_in_chrome_code
  • Revision title: Using the DOM File API in chrome code
  • Revision id: 68580
  • Created:
  • Creator: madarche
  • Is current revision? No
  • Comment 13 words added, 2 words removed

Revision Content

{{ gecko_minversion_header("6.0") }}

If you want to use the DOM File API in chrome code, you can do so without restriction. In fact, you get one bonus feature: you can create {{ domxref("File") }} objects specifying the path of the file on the user's computer. This only works from privileged code, so web content can't do it. This protects users from the inherent security risks associated with allowing web content free access to the contents of their disks. If you pass a path to the {{ domxref("File") }} constructor from unprivileged code (such as web content), an exception will be thrown.

Accessing a file by hard-coded pathname

To reference a file by its path, you can simply use a string literal:

var file = File("path/to/some/file");

Cross platform note: However using hard-coded paths raises cross platform issues since it uses a platform-dependent path separator (here "/"). In the XUL/Mozilla platform there isn't sadly an equivalent to Java File.pathSeparator (the system-dependent path-separator character). So the good practice is to avoid trying to determine and to use the path separator at all. Instead, use the nsIFile::append() method as explained in the next section.

Accessing files in a special directory

You can also use the directory service to obtain and build the path to a file to access. For example, let's say your add-on needs to access a file in the user's profile. You can do so like this:

var dsFile = Components.classes["@mozilla.org/file/directory_service;1"]
                    .getService(Components.interfaces.nsIProperties)
                    .get("ProfD", Components.interfaces.nsIFile);

dsFile.append("myfilename.txt");

var file = File(dsFile.path);

This uses the directory service to locate the profile directory (with the location key "ProfD", see below for more details), then appends the name of the file we want to work with by calling {{ ifmethod("nsIFile", "append") }}. Finally, we instantiate the {{ domxref("File") }} object by passing the string returned by {{ ifmethod("nsIFile", "path") }} to the {{ domxref("File") }} constructor.

You can even simplify this further! You can actually pass the {{ interface("nsIFile") }} object itself directly to the File constructor, resulting in the following code:

var dsFile = Components.classes["@mozilla.org/file/directory_service;1"]
                    .getService(Components.interfaces.nsIProperties)
                    .get("ProfD", Components.interfaces.nsIFile);

dsFile.append("myfilename.txt");

var file = File(dsFile);

Other such keys as the "ProfD" key are available, check the known locations.

See also

Revision Source

<p>{{ gecko_minversion_header("6.0") }}</p>
<p>If you want to use the <a href="/en/Using_files_from_web_applications" title="en/Using files from web applications">DOM File API</a> in chrome code, you can do so without restriction. In fact, you get one bonus feature: you can create {{ domxref("File") }} objects specifying the path of the file on the user's computer. This only works from privileged code, so web content can't do it. This protects users from the inherent security risks associated with allowing web content free access to the contents of their disks. If you pass a path to the {{ domxref("File") }} constructor from unprivileged code (such as web content), an exception will be thrown.</p>
<h2>Accessing a file by hard-coded pathname</h2>
<p>To reference a file by its path, you can simply use a string literal:</p>
<pre>var file = File("path/to/some/file");
</pre>
<p>Cross platform note: However using hard-coded paths raises cross platform issues since it uses a platform-dependent path separator (here "/"). In the XUL/Mozilla platform there isn't sadly an equivalent to Java <code>File.pathSeparator</code> (the system-dependent path-separator character<code>).</code> So the good practice is to avoid trying to determine and to use the path separator at all. Instead, use the nsIFile::append() method as explained in the next section.</p><h2>Accessing files in a special directory</h2>
<p>You can also use the directory service to obtain and build the path to a file to access. For example, let's say your add-on needs to access a file in the user's profile. You can do so like this:</p>
<pre class="brush: js">var dsFile = Components.classes["@mozilla.org/file/directory_service;1"]
                    .getService(Components.interfaces.nsIProperties)
                    .get("ProfD", Components.interfaces.nsIFile);

dsFile.append("myfilename.txt");

var file = File(dsFile.path);
</pre>
<p>This uses the directory service to locate the profile directory (with the location key "ProfD", see below for more details), then appends the name of the file we want to work with by calling {{ ifmethod("nsIFile", "append") }}. Finally, we instantiate the {{ domxref("File") }} object by passing the string returned by {{ ifmethod("nsIFile", "path") }} to the {{ domxref("File") }} constructor.</p>
<p>You can even simplify this further! You can actually pass the {{ interface("nsIFile") }} object itself directly to the <code>File</code> constructor, resulting in the following code:</p>
<pre class="brush: js">var dsFile = Components.classes["@mozilla.org/file/directory_service;1"]
                    .getService(Components.interfaces.nsIProperties)
                    .get("ProfD", Components.interfaces.nsIFile);

dsFile.append("myfilename.txt");

var file = File(dsFile);</pre>
<p>Other such keys as the "ProfD" key are available, check the <a href="/en/Using_nsIDirectoryService#Known_Locations" title="https://developer.mozilla.org/en/Using_nsIDirectoryService#Known_Locations">known locations</a>.</p><h2>See also</h2>
<ul> <li><a href="/en/Using_files_from_web_applications" title="en/Using files from web applications">Using files from web applications</a></li> <li>{{ domxref("File") }}</li> <li>{{ interface("nsIDirectoryService") }}</li> <li>{{ interface("nsIFile") }}</li>
</ul>
Revert to this revision