--- title: File.webkitRelativePath slug: Web/API/File/webkitRelativePath translation_of: Web/API/File/webkitRelativePath --- <p>{{APIRef("File API")}}{{non-standard_header}}</p> <p><code><strong>File.webkitRelativePath</strong></code> 是只读属性,包含 {{domxref("USVString")}},它规定了文件的路径,相对于用户在 {{HTMLElement("input")}} 元素中选择的目录,这个元素设置了 {{htmlattrxref("webkitdirectory", "input")}} 属性。</p> <h2 id="语法">语法</h2> <pre class="syntaxbox"> <code><em>relativePath = File</em></code>.webkitRelativePath</pre> <h3 id="值">值</h3> <p>{{domxref("USVString")}} 包含文件路径,相对于用户所选的祖先目录。</p> <h2 id="Example" name="Example">示例</h2> <p>这个示例展示了目录选择器,它让用户选择一个或多个目录。{{event("change")}} 事件触发时,所选目录包含的所有文件的列表,会生成并展示,带有所选目录的层次结构。</p> <h3 id="HTML_内容">HTML 内容</h3> <pre class="brush: html"><input type="file" id="filepicker" name="fileList" webkitdirectory multiple /> <ul id="listing"></ul></pre> <h3 id="JavaScript_内容">JavaScript 内容</h3> <pre class="brush: js">document.getElementById("filepicker").addEventListener("change", function(event) { let output = document.getElementById("listing"); let files = event.target.files; for (let i=0; i<files.length; i++) { let item = document.createElement("li"); item.innerHTML = files[i].webkitRelativePath; output.appendChild(item); }; }, false); </pre> <h3 id="结果">结果</h3> <p>{{ EmbedLiveSample('Example') }}</p> <p>特别提醒:假设文件路径是 C:\f1\f2\f3\file.txt, 用户选择的是f1 文件夹,则Chrome、Firefox、Edge 都能正确返回 f2/f3/file.txt值。而国产的QQ浏览器、360浏览器、UC浏览器、搜狗浏览器都只能返回 f3/file.txt。也就是国产浏览器调用webkitRelativePath返回的结果都不会是你希望得到的路径,请注意使用时的细微差距。</p> <h2 id="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td>{{ SpecName('File System API', '#dom-file-webkitrelativepath', 'webkitRelativePath') }}</td> <td>{{ Spec2('File System API') }}</td> <td>Initial specification.</td> </tr> </tbody> </table> <p>这个 API 没有官方的 W3C 或者 WHATWG 规范。</p> <h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> <p>{{ CompatibilityTable }}</p> <div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari (WebKit)</th> </tr> <tr> <td>Basic support</td> <td>13 {{ property_prefix("webkit") }}</td> <td>{{ CompatGeckoDesktop(49) }}</td> <td>{{ CompatNo }}</td> <td>{{ CompatNo }}</td> <td>{{ CompatNo }}</td> </tr> </tbody> </table> </div> <div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Chrome for Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatNo }}</td> <td>0.16 {{ property_prefix("webkit") }}</td> <td>{{ CompatGeckoMobile(49) }}</td> <td>{{ CompatNo }}</td> <td>{{ CompatNo }}</td> <td>{{ CompatNo }}</td> </tr> </tbody> </table> </div> <h2 id="另见">另见</h2> <ul> <li><a href="/en-US/docs/Web/API/File_and_Directory_Entries_API">文件和目录条目 API</a></li> <li>{{domxref("HTMLInputElement.webkitEntries")}}</li> <li>{{domxref("HTMLInputElement.webkitdirectory")}}</li> </ul>