js-apis-file-fileuri.md 1.8 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.file.fileuri (File URI)
A
Annie_wang 已提交
2 3 4 5 6 7 8 9 10 11

The **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.

> **NOTE**
>
> 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.

## Modules to Import

```js
A
Annie_wang 已提交
12
import fileuri from "@ohos.file.fileuri";
A
Annie_wang 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
```

Before using this module, you need to obtain the path of the file in the application sandbox. The following is an example:

 ```js
import UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage) {
        let context = this.context;
        let pathDir = context.filesDir;
    }
}
 ```

## fileUri.getUriFromPath

getUriFromPath(path: string): string

Obtains the URI of a file in synchronous mode.

**System capability**: SystemCapability.FileManagement.AppFileService

**Parameters**

| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
| path   | string | Yes  | Path of the file in the application sandbox.|

**Return value**

| Type                          | Description        |
| ---------------------------- | ---------- |
| string | File URI obtained.|

**Error codes** 

For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID                    | Error Message       |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid |


**Example**

  ```js
let filePath = pathDir + "test.txt";
A
Annie_wang 已提交
60
let uri = fileuri.getUriFromPath(filePath);
A
Annie_wang 已提交
61
  ```