• 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```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| Converts the URI to the corresponding sandbox path. 1. During URI-to-path conversion, the ASCII code in the URI is decoded and then concatenated to the original position. The URI generated by a non-system API may contain characters beyond the ASCII code parsing range. As a result, the string cannot be concatenated. 2. The conversion is performed based on the string replacement rule specified by the system (the rule may change with the system evolution). During the conversion, the path is not verified, and the conversion result may not be accessible.|
38| name<sup>10+</sup> | string | Yes| Obtains the file name based on the given URI. (If the file name contains the ASCII code, the file name will be decoded and then concatenated.)<br>**Atomic service API**: This API can be used in atomic services since API version 15.|
39
40### constructor<sup>10+</sup>
41
42constructor(uriOrPath: string)
43
44A constructor used to create a **FileUri** instance.
45
46**Atomic service API**: This API can be used in atomic services since API version 15.
47
48**System capability**: SystemCapability.FileManagement.AppFileService
49
50**Parameters**
51
52| Name| Type| Mandatory| Description|
53| -------- | -------- | -------- |--------|
54| uriOrPath | string | Yes| URI or path. The following types of URIs are available:<br>- Application sandbox URI: file://\<bundleName>/\<sandboxPath><br>- User directory file URI: file://docs/storage/Users/currentUser/\<publicPath><br>- User directory media URI: file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName>|
55
56**Error codes**
57
58For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
59| ID| Error Message       |
60| -------- | ---------- |
61| 13900005 | I/O error |
62| 13900042 | Unknown error |
63| 13900020 | Invalid argument |
64| 14300002 | Invalid uri |
65
66**Example**
67
68  ```ts
69  let path = pathDir + '/test';
70  let uri = fileUri.getUriFromPath(path);  // file://<packageName>/data/storage/el2/base/haps/entry/files/test
71  let fileUriObject = new fileUri.FileUri(uri);
72  console.info("The name of FileUri is " + fileUriObject.name);
73  ```
74
75### toString<sup>10+</sup>
76
77toString(): string
78
79**System capability**: SystemCapability.FileManagement.AppFileService
80
81Converts this URI into a string.
82
83**Return value**
84
85| Type| Description|
86| -------- | -------- |
87| string | URI in the string format.|
88
89**Example**
90
91  ```ts
92  let path = pathDir + '/test';
93  let fileUriObject = new fileUri.FileUri(path);
94  console.info("The uri of FileUri is " + fileUriObject.toString());
95  ```
96
97### getFullDirectoryUri<sup>11+</sup>
98
99getFullDirectoryUri(): string
100
101Obtains the URI of the path. If the URI points to a file, the URI of the path is returned. If the URI points to a directory, the original string is returned without processing. If the file specified by the URI does not exist or the attribute fails to be obtained, an empty string is returned.
102
103For 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.
104
105If the current FileUri points to a directory, the URI of the current path is returned.
106
107**Atomic service API**: This API can be used in atomic services since API version 15.
108
109**System capability**: SystemCapability.FileManagement.AppFileService
110
111**Return value**
112
113| Type| Description|
114| --------- |--------|
115| string | URI of the directory where the current file is located or URI of the current folder.|
116
117**Error codes**
118
119For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
120
121| ID| Error Message|
122| -------- |--------|
123| 13900002 | No such file or directory |
124| 13900012 | Permission denied         |
125| 13900042 | Unknown error             |
126
127**Example**
128
129  ```ts
130  import { BusinessError } from '@kit.BasicServicesKit';
131  try {
132    let path = pathDir + '/test.txt';
133    let fileUriObject = new fileUri.FileUri(path);
134    let directoryUri = fileUriObject.getFullDirectoryUri();
135    console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`);
136  } catch (error) {
137    console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`);
138  }
139  ```
140
141### isRemoteUri<sup>12+</sup>
142
143isRemoteUri(): boolean
144
145Checks whether this URI is a remote URI.
146
147**Atomic service API**: This API can be used in atomic services since API version 15.
148
149**System capability**: SystemCapability.FileManagement.AppFileService
150
151**Return value**
152
153| Type| Description|
154| -------- |---------|
155| 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.|
156
157**Error codes**
158
159For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
160
161| ID| Error Message|
162| --------- |--------|
163| 13900042 | Unknown error|
164
165**Example**
166
167  ```ts
168  import { BusinessError } from '@kit.BasicServicesKit';
169  function isRemoteUriExample() {
170    let uri = "file://com.example.demo/data/stroage/el2/base/test.txt?networkid=xxxx";// ?networkid identifies a remote device.
171    let fileUriObject = new fileUri.FileUri(uri);
172    let ret = fileUriObject.isRemoteUri();
173    if (ret) {
174        console.log(`It is a remote uri.`);
175    }
176  }
177  ```
178
179## fileUri.getUriFromPath
180
181getUriFromPath(path: string): string
182
183The URI of the application is generated based on the input path. When a path is converted to a URI, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and combined into the URI.
184
185**Atomic service API**: This API can be used in atomic services since API version 15.
186
187**System capability**: SystemCapability.FileManagement.AppFileService
188
189**Parameters**
190
191| Name| Type  | Mandatory| Description|
192| ------ | ------ | ---- | ------- |
193| path   | string | Yes  | Application sandbox path of the file.|
194
195**Return value**
196
197  | Type| Description|
198  | ------- |------|
199  | string | The URI of the application is generated based on the input path. When a path is converted to a URI, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and combined into the URI.|
200
201**Error codes**
202
203For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
204| ID| Error Message       |
205| ---------- | ---------- |
206| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types |
207
208**Example**
209
210  ```ts
211  let filePath = pathDir + "/test";
212  let uri = fileUri.getUriFromPath(filePath);
213  ```
214