js-apis-file-fileuri.md 4.0 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
```

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

A
Annie_wang 已提交
17 18
**Stage Model**

A
Annie_wang 已提交
19 20 21 22 23 24 25 26 27 28 29
 ```js
import UIAbility from '@ohos.app.ability.UIAbility';

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

A
Annie_wang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
**FA Model**

 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 
 let context = featureAbility.getContext();
 context.getFilesDir().then((data) => {
      let pathDir = data;
 })
 ```

For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context).

A
Annie_wang 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
## FileUri<sup>10+</sup>

### Attributes

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

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |    
| path<sup>10+</sup> | string | Yes| No| Path of the file corresponding to the URI.|
| name<sup>10+</sup> | string | Yes| No| Name of the file.|

### constructor<sup>10+</sup>

constructor(uriOrPath: string)

A constructor used to create a **FileUri** instance.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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>**|

**Error codes**

For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID                    | Error Message       |
| ---------------------------- | ---------- |
| 13900005 | I/O error |
| 13900042 | Unknown error |

**Example**

```js
let path = pathDir + '/test';
let uri = fileuri.getUriFromPath(filePath);  // file://<packageName>/data/storage/el2/base/haps/entry/files/test
let fileUriObject = new fileuri.FileUri(uri);
console.info("The name of FileUri is " + fileUriObject.name);
```

### toString<sup>10+</sup>

toString(): string

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

Obtains the URI of the string type.

**Return value**

| Type| Description|
| -------- | -------- |
| string | URI of the string type obtained.|

**Example**

```js
let path = pathDir + '/test';
let fileUriObject = new fileuri.FileUri(path);
console.info("The uri of FileUri is " + fileUriObject.toString());
```

A
Annie_wang 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
## 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**

A
Annie_wang 已提交
123 124 125
  | Type                          | Description        |
  | ---------------------------- | ---------- |
  | string | File URI obtained.|
A
Annie_wang 已提交
126 127 128 129 130 131 132 133 134 135 136 137

**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 已提交
138
let uri = fileuri.getUriFromPath(filePath);
A
Annie_wang 已提交
139
  ```