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 "@ohos.file.fileuri"; 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 '@ohos.app.ability.UIAbility'; 19 import window from '@ohos.window'; 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### Attributes 32 33**System capability**: SystemCapability.FileManagement.AppFileService 34 35| Name| Type| Readable| Writable| Description| 36| -------- | -------- | -------- | -------- | -------- | 37| path<sup>10+</sup> | string | Yes| No| Path of the file. | 38| name<sup>10+</sup> | string | Yes| No| 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](../errorcodes/errorcode-filemanagement.md). 57| ID | Error Message | 58| ---------------------------- | ---------- | 59| 13900005 | I/O error | 60| 13900042 | Unknown error | 61 62**Example** 63 64 ```ts 65 let path = pathDir + '/test'; 66 let uri = fileUri.getUriFromPath(path); // file://<packageName>/data/storage/el2/base/haps/entry/files/test 67 let fileUriObject = new fileUri.FileUri(uri); 68 console.info("The name of FileUri is " + fileUriObject.name); 69 ``` 70 71### toString<sup>10+</sup> 72 73toString(): string 74 75**System capability**: SystemCapability.FileManagement.AppFileService 76 77Obtains the URI of the string type. 78 79**Return value** 80 81| Type| Description| 82| -------- | -------- | 83| string | URI of the string type obtained.| 84 85**Example** 86 87 ```ts 88 let path = pathDir + '/test'; 89 let fileUriObject = new fileUri.FileUri(path); 90 console.info("The uri of FileUri is " + fileUriObject.toString()); 91 ``` 92 93### getFullDirectoryUri<sup>11+</sup> 94 95getFullDirectoryUri(): string 96 97Obtains the URI of the full directory of this file or folder. 98 99For 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. 100 101For a folder, this API returns the URI of the folder. 102 103**System capability**: SystemCapability.FileManagement.AppFileService 104 105**Return value** 106 107| Type | Description | 108| --------------------- |-----------------------------------| 109| string | URI of the directory where the current file is located or URI of the current folder.| 110 111**Error codes** 112 113For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 114 115| ID | Error Message | 116| ---------------------------- |---------------------------| 117| 13900002 | No such file or directory | 118| 13900012 | Permission denied | 119| 13900042 | Unknown error | 120 121**Example** 122 123 ```ts 124 import { BusinessError } from '@ohos.base'; 125 try { 126 let path = pathDir + '/test.txt'; 127 let fileUriObject = new fileUri.FileUri(path); 128 let directoryUri = fileUriObject.getFullDirectoryUri(); 129 console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`); 130 } catch (error) { 131 console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`); 132 } 133 ``` 134 135## fileUri.getUriFromPath 136 137getUriFromPath(path: string): string 138 139Obtains the URI based on a file path. This API returns the result synchronously. 140 141**System capability**: SystemCapability.FileManagement.AppFileService 142 143**Parameters** 144 145| Name| Type | Mandatory| Description | 146| ------ | ------ | ---- | -------------------------- | 147| path | string | Yes | Application sandbox path of the file. | 148 149**Return value** 150 151| Type | Description | 152| ---------------------------- | ---------- | 153| string | File URI obtained.| 154 155**Error codes** 156 157For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md). 158| ID | Error Message | 159| ---------------------------- | ---------- | 160| 401 | The input parameter is invalid | 161 162**Example** 163 164 ```ts 165 let filePath = pathDir + "/test"; 166 let uri = fileUri.getUriFromPath(filePath); 167 ``` 168