mozilla

Revision 68575 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: 68575
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 1 words added, 1 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");

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 ("ProfD"), 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.

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>
<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 ("ProfD"), 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>
<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