• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.fileuri (File URI)
2
3The **fileUri** module allows the uniform resource identifier (URI) of a file to be obtained based on the file path. With the file URI, you can use the APIs provided by [@ohos.file.fs](js-apis-file-fs.md) to operate the file.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import fileuri from "@ohos.file.fileuri";
13```
14
15Before using this module, you need to obtain the path of the file in the application sandbox. The following is an example:
16
17 ```js
18import UIAbility from '@ohos.app.ability.UIAbility';
19
20export default class EntryAbility extends UIAbility {
21    onWindowStageCreate(windowStage) {
22        let context = this.context;
23        let pathDir = context.filesDir;
24    }
25}
26 ```
27
28## fileUri.getUriFromPath
29
30getUriFromPath(path: string): string
31
32Obtains the URI of a file in synchronous mode.
33
34**System capability**: SystemCapability.FileManagement.AppFileService
35
36**Parameters**
37
38| Name| Type  | Mandatory| Description                      |
39| ------ | ------ | ---- | -------------------------- |
40| path   | string | Yes  | Path of the file in the application sandbox.|
41
42**Return value**
43
44| Type                          | Description        |
45| ---------------------------- | ---------- |
46| string | File URI obtained.|
47
48**Error codes**
49
50For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
51| ID                    | Error Message       |
52| ---------------------------- | ---------- |
53| 401 | The input parameter is invalid |
54
55
56**Example**
57
58  ```js
59let filePath = pathDir + "test.txt";
60let uri = fileuri.getUriFromPath(filePath);
61  ```
62