• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.fileuri (文件URI)
2
3该模块提供通过PATH获取文件统一资源标志符(Uniform Resource Identifier,URI),后续可通过使用[@ohos.file.fs](js-apis-file-fs.md)进行相关open、read、write等操作,实现文件分享。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import fileuri from "@ohos.file.fileuri";
13```
14
15使用该功能模块前,需要先获取其应用沙箱路径,开发示例如下:
16
17**Stage模型**
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模型**
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
42FA模型context的具体获取方法参见[FA模型](js-apis-inner-app-context.md#Context模块)。
43
44## FileUri<sup>10+</sup>
45
46### 属性
47
48**系统能力**:SystemCapability.FileManagement.AppFileService
49
50| 名称 | 类型 | 可读 | 可写 | 说明 |
51| -------- | -------- | -------- | -------- | -------- |
52| path<sup>10+</sup> | string | 是 | 否 | 获取FileUri对应路径名 |
53| name<sup>10+</sup> | string | 是 | 否 | 获取FileUri对应文件名 |
54
55### constructor<sup>10+</sup>
56
57constructor(uriOrPath: string)
58
59constructor是FileUri的构造函数。
60
61**系统能力:** SystemCapability.FileManagement.AppFileService
62
63**参数:**
64
65| 参数名 | 类型 | 必填 | 说明 |
66| -------- | -------- | -------- | -------- |
67| uriOrPath | string | 是 | uri或路径。uri类型:<br/>-&nbsp; 应用沙箱URI:file://\<bundleName>/\<sandboxPath> <br/>-&nbsp; 公共目录文件类URI:file://docs/storage/Users/currentUser/\<publicPath> <br/>-&nbsp; 公共目录媒体类URI:file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName> |
68
69**错误码:**
70
71以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。
72| 错误码ID                     | 错误信息        |
73| ---------------------------- | ---------- |
74| 13900005 | I/O error |
75| 13900042 | Unknown error |
76
77**示例:**
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**系统能力:** SystemCapability.FileManagement.AppFileService
91
92返回字符串类型uri。
93
94**返回值:**
95
96| 类型 | 说明 |
97| -------- | -------- |
98| string | 返回字符串类型uri |
99
100**示例:**
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
112以同步方法获取文件URI。
113
114**系统能力**:SystemCapability.FileManagement.AppFileService
115
116**参数:**
117
118| 参数名 | 类型   | 必填 | 说明                       |
119| ------ | ------ | ---- | -------------------------- |
120| path   | string | 是   | 文件的沙箱路径 |
121
122**返回值:**
123
124  | 类型                           | 说明         |
125  | ---------------------------- | ---------- |
126  | string | 返回文件URI |
127
128**错误码:**
129
130以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。
131| 错误码ID                     | 错误信息        |
132| ---------------------------- | ---------- |
133| 401 | The input parameter is invalid |
134
135**示例:**
136
137  ```ts
138  let filePath = pathDir + "/test";
139  let uri = fileuri.getUriFromPath(filePath);
140  ```
141