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