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 path of the file in the application sandbox. The following is an example: 16 17**Stage Model** 18 19 ```ts 20 import UIAbility from '@ohos.app.ability.UIAbility'; 21 import window from '@ohos.window'; 22 23 export default class EntryAbility extends UIAbility { 24 onWindowStageCreate(windowStage: window.WindowStage) { 25 let context = this.context; 26 let pathDir = context.filesDir; 27 } 28 } 29 ``` 30 31**FA Model** 32 33 ```js 34 import featureAbility from '@ohos.ability.featureAbility'; 35 36 let context = featureAbility.getContext(); 37 context.getFilesDir().then((data) => { 38 let pathDir = data; 39 }) 40 ``` 41 42For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context). 43 44## FileUri<sup>10+</sup> 45 46### Attributes 47 48**System capability**: SystemCapability.FileManagement.AppFileService 49 50| Name| Type| Readable| Writable| Description| 51| -------- | -------- | -------- | -------- | -------- | 52| path<sup>10+</sup> | string | Yes| No| Path of the file corresponding to the URI.| 53| name<sup>10+</sup> | string | Yes| No| Name of the file.| 54 55### constructor<sup>10+</sup> 56 57constructor(uriOrPath: string) 58 59A constructor used to create a **FileUri** instance. 60 61**System capability**: SystemCapability.FileManagement.AppFileService 62 63**Parameters** 64 65| Name| Type| Mandatory| Description| 66| -------- | -------- | -------- | -------- | 67| uriOrPath | string | Yes| URI or path. The following types of URIs are available:<br>- Application sandbox URI: **file://\<bundleName>/\<sandboxPath>**<br>- URI of the user's document: **file://docs/storage/Users/currentUser/\<publicPath>**<br>- URI of the user's media asset: **file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName>**| 68 69**Error codes** 70 71For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 72| ID | Error Message | 73| ---------------------------- | ---------- | 74| 13900005 | I/O error | 75| 13900042 | Unknown error | 76 77**Example** 78 79 ```ts 80 let path = pathDir + '/test'; 81 let uri = fileuri.getUriFromPath(filePath); // file://<packageName>/data/storage/el2/base/haps/entry/files/test 82 let fileUriObject = new fileuri.FileUri(uri); 83 console.info("The name of FileUri is " + fileUriObject.name); 84 ``` 85 86### toString<sup>10+</sup> 87 88toString(): string 89 90**System capability**: SystemCapability.FileManagement.AppFileService 91 92Obtains the URI of the string type. 93 94**Return value** 95 96| Type| Description| 97| -------- | -------- | 98| string | URI of the string type obtained.| 99 100**Example** 101 102 ```ts 103 let path = pathDir + '/test'; 104 let fileUriObject = new fileuri.FileUri(path); 105 console.info("The uri of FileUri is " + fileUriObject.toString()); 106 ``` 107 108## fileuri.getUriFromPath 109 110getUriFromPath(path: string): string 111 112Obtains the URI of a file in synchronous mode. 113 114**System capability**: SystemCapability.FileManagement.AppFileService 115 116**Parameters** 117 118| Name| Type | Mandatory| Description | 119| ------ | ------ | ---- | -------------------------- | 120| path | string | Yes | Path of the file in the application sandbox.| 121 122**Return value** 123 124 | Type | Description | 125 | ---------------------------- | ---------- | 126 | string | File URI obtained.| 127 128**Error codes** 129 130For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 131| ID | Error Message | 132| ---------------------------- | ---------- | 133| 401 | The input parameter is invalid | 134 135**Example** 136 137 ```ts 138 let filePath = pathDir + "/test"; 139 let uri = fileuri.getUriFromPath(filePath); 140 ``` 141