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```ts
12import { fileUri } from '@kit.CoreFileKit';
13```
14
15Before using this module, you need to obtain the application sandbox path of the file. The following is an example:
16
17  ```ts
18  import { UIAbility } from '@kit.AbilityKit';
19  import { window } from '@kit.ArkUI';
20
21  export default class EntryAbility extends UIAbility {
22    onWindowStageCreate(windowStage: window.WindowStage) {
23      let context = this.context;
24      let pathDir = context.filesDir;
25    }
26  }
27  ```
28
29## FileUri<sup>10+</sup>
30
31### Properties
32
33**System capability**: SystemCapability.FileManagement.AppFileService
34
35| Name| Type| Mandatory| Description|
36| -------- | --------| -------- | -------- |
37| path<sup>10+</sup> | string | Yes| Path of the file.|
38| name<sup>10+</sup> | string | Yes| Name of the file.|
39
40### constructor<sup>10+</sup>
41
42constructor(uriOrPath: string)
43
44A constructor used to create a **FileUri** instance.
45
46**System capability**: SystemCapability.FileManagement.AppFileService
47
48**Parameters**
49
50| Name| Type| Mandatory| Description|
51| -------- | -------- | -------- | -------- |
52| uriOrPath | string | Yes| URI or path. The following types of URIs are available:<br>- Application sandbox URI: **file://\<bundleName>/\<sandboxPath>**<br>- User file URI: **file://docs/storage/Users/currentUser/\<publicPath>**<br>- User media asset URI: **file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName>**|
53
54**Error codes**
55
56For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
57| ID                    | Error Message       |
58| ---------------------------- | ---------- |
59| 13900005 | I/O error |
60| 13900042 | Unknown error |
61| 13900020 | invalid argument |
62| 13900002 | invalid uri |
63
64**Example**
65
66  ```ts
67  let path = pathDir + '/test';
68  let uri = fileUri.getUriFromPath(path);  // file://<packageName>/data/storage/el2/base/haps/entry/files/test
69  let fileUriObject = new fileUri.FileUri(uri);
70  console.info("The name of FileUri is " + fileUriObject.name);
71  ```
72
73### toString<sup>10+</sup>
74
75toString(): string
76
77**System capability**: SystemCapability.FileManagement.AppFileService
78
79Converts this URI into a string.
80
81**Return value**
82
83| Type| Description|
84| -------- | -------- |
85| string | URI obtained, in the string format.|
86
87**Example**
88
89  ```ts
90  let path = pathDir + '/test';
91  let fileUriObject = new fileUri.FileUri(path);
92  console.info("The uri of FileUri is " + fileUriObject.toString());
93  ```
94
95### getFullDirectoryUri<sup>11+</sup>
96
97getFullDirectoryUri(): string
98
99Obtains the URI of the full directory of this file or folder.
100
101For a file, this API returns the URI of the directory where the file is located. For example, **xxx** will be returned for the **xxx/example.txt** file.
102
103For a folder, this API returns the URI of the folder.
104
105**System capability**: SystemCapability.FileManagement.AppFileService
106
107**Return value**
108
109| Type                 | Description                               |
110| --------------------- |-----------------------------------|
111| string | URI of the directory where the current file is located or URI of the current folder.|
112
113**Error codes**
114
115For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
116
117| ID                    | Error Message                     |
118| ---------------------------- |---------------------------|
119| 13900002 | No such file or directory |
120| 13900012 | Permission denied         |
121| 13900042 | Unknown error             |
122
123**Example**
124
125  ```ts
126  import { BusinessError } from '@kit.BasicServicesKit';
127  try {
128    let path = pathDir + '/test.txt';
129    let fileUriObject = new fileUri.FileUri(path);
130    let directoryUri = fileUriObject.getFullDirectoryUri();
131    console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`);
132  } catch (error) {
133    console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`);
134  }
135  ```
136
137### isRemoteUri<sup>12+</sup>
138
139isRemoteUri(): boolean
140
141Checks whether this URI is a remote URI.
142
143**System capability**: SystemCapability.FileManagement.AppFileService
144
145**Return value**
146
147| Type                 | Description                               |
148| --------------------- |-----------------------------------|
149| boolean | - Returns **true** if the URI points to a remote file or folder, for example, **xxx/example.txt? networkid=xxx**.<br>- Returns **false** if the URI points to a local file or folder.|
150
151**Error codes**
152
153For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
154
155| ID                    | Error Message                     |
156| ---------------------------- |---------------------------|
157| 13900042 | Unknown error             |
158
159**Example**
160
161  ```ts
162  import { BusinessError } from '@kit.BasicServicesKit';
163  function isRemoteUriExample() {
164    let uri = "file://com.example.demo/data/stroage/el2/base/test.txt?networkid=xxxx";// ?networkid identifies a remote device.
165    let fileUriObject = new fileUri.FileUri(uri);
166    let ret = fileUriObject.isRemoteUri();
167    if (ret) {
168        console.log(`It is a remote uri.`);
169    }
170  }
171  ```
172
173## fileUri.getUriFromPath
174
175getUriFromPath(path: string): string
176
177Obtains the URI based on a file path. This API returns the result synchronously.
178
179**System capability**: SystemCapability.FileManagement.AppFileService
180
181**Parameters**
182
183| Name| Type  | Mandatory| Description                      |
184| ------ | ------ | ---- | -------------------------- |
185| path   | string | Yes  | Application sandbox path of the file.|
186
187**Return value**
188
189  | Type                          | Description        |
190  | ---------------------------- | ---------- |
191  | string | File URI obtained.|
192
193**Error codes**
194
195For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
196| ID                    | Error Message       |
197| ---------------------------- | ---------- |
198| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types |
199
200**Example**
201
202  ```ts
203  let filePath = pathDir + "/test";
204  let uri = fileUri.getUriFromPath(filePath);
205  ```
206