diff options
Diffstat (limited to 'files/zh-tw/extensions')
-rw-r--r-- | files/zh-tw/extensions/index.html | 9 | ||||
-rw-r--r-- | files/zh-tw/extensions/using_the_dom_file_api_in_chrome_code/index.html | 49 |
2 files changed, 58 insertions, 0 deletions
diff --git a/files/zh-tw/extensions/index.html b/files/zh-tw/extensions/index.html new file mode 100644 index 0000000000..63a7457aaa --- /dev/null +++ b/files/zh-tw/extensions/index.html @@ -0,0 +1,9 @@ +--- +title: Extensions +slug: Extensions +--- +<p> </p> + +<p>This page was auto-generated because a user created a sub-page to this page.</p> + +<p> </p> diff --git a/files/zh-tw/extensions/using_the_dom_file_api_in_chrome_code/index.html b/files/zh-tw/extensions/using_the_dom_file_api_in_chrome_code/index.html new file mode 100644 index 0000000000..e4bd4f169c --- /dev/null +++ b/files/zh-tw/extensions/using_the_dom_file_api_in_chrome_code/index.html @@ -0,0 +1,49 @@ +--- +title: 使用 DOM 檔案 API 再FireFox外觀代碼上 +slug: Extensions/Using_the_DOM_File_API_in_chrome_code +translation_of: Extensions/Using_the_DOM_File_API_in_chrome_code +--- +<p>{{ gecko_minversion_header("6.0") }}</p> + +<p>如果你想使用如果你想使用 <a href="/en/Using_files_from_web_applications" title="en/Using files from web applications">DOM 檔案 API</a> 在FireFox外觀代碼上面,你可以沒有任何限制的這樣做。事實上你得到了一個特別的功能。你可以建立你可以建立 {{ domxref("File") }} 物件 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 id="Accessing_a_file_by_hard-coded_pathname">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 id="Accessing_files_in_a_special_directory">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> + +<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> + +<h2 id="See_also">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> |