• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.fileShare (File Sharing)
2
3The **fileShare** module provides APIs for granting the access permissions on a user file to another application by the Uniform Resource Identifier (URI). Then, the authorized application can access the file by using the APIs provided by [@ohos.file.fs](js-apis-file-fs.md).
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```js
12import fileShare from '@ohos.fileShare';
13```
14
15## fileShare.grantUriPermission
16
17grantUriPermission(uri: string, bundleName: string, mode: number, callback: AsyncCallback<void>): void
18
19Grants permissions on a user file by the URI to an application. This API uses an asynchronous callback to return the result.
20
21**Required permissions**: ohos.permission.WRITE_MEDIA
22
23**System API**: This is a system API.
24
25**System capability**: SystemCapability.FileManagement.AppFileService
26
27**Parameters**
28
29| Name| Type  | Mandatory| Description                      |
30| ------ | ------ | ---- | -------------------------- |
31| uri   | string | Yes  | URI of a user file.|
32| bundleName   | string | Yes  | Bundle name of the application to be grated with the permissions.|
33| mode   | number | Yes  | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).<br>**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file. <br>**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
34| callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked to return the result.                            |
35
36**Error codes**
37
38For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
39
40| ID                    | Error Message       |
41| ---------------------------- | ---------- |
42| 201 | Permission verification failed |
43| 202 | The caller is not a system application |
44| 401 | The input parameter is invalid |
45| 143000001 | IPC error |
46
47
48**Example**
49
50  ```js
51import wantConstant from '@ohos.app.ability.wantConstant';
52
53
54let uri = 'datashare:///media/image/8';
55let bundleName = 'com.demo.test';
56try {
57    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err) => {
58        if (err) {
59            console.error("grantUriPermission failed with error: " + err);
60            return;
61        }
62        console.info("grantUriPermission success!");
63    });
64} catch (error) {
65    console.error("grantUriPermission failed with error:" + error);
66}
67  ```
68
69
70## fileShare.grantUriPermission
71
72grantUriPermission(uri: string, bundleName: string, mode: number): Promise&lt;void&gt;
73
74Grants permissions on a user file by the URI to an application. This API uses a promise to return the result.
75
76**Required permissions**: ohos.permission.WRITE_MEDIA
77
78**System API**: This is a system API.
79
80**System capability**: SystemCapability.FileManagement.AppFileService
81
82**Parameters**
83
84| Name| Type  | Mandatory| Description                      |
85| ------ | ------ | ---- | -------------------------- |
86| uri   | string | Yes  | URI of a user file.|
87| bundleName   | string | Yes  | Bundle name of the application to be grated with the permissions.|
88| mode   | number | Yes  | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).<br>**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file. <br>**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
89
90**Return value**
91
92| Type                          | Description        |
93| ---------------------------- | ---------- |
94| Promise&lt;void&gt; | Promise that returns no value.|
95
96
97**Error codes**
98
99For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
100
101| ID                    | Error Message       |
102| ---------------------------- | ---------- |
103| 201 | Permission verification failed |
104| 202 | The caller is not a system application |
105| 401 | The input parameter is invalid |
106| 143000001 | IPC error |
107
108
109**Example**
110
111  ```js
112import wantConstant from '@ohos.app.ability.wantConstant';
113
114let uri = 'datashare:///media/image/8';
115let bundleName = 'com.demo.test';
116try {
117    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
118      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(function () {
119        console.info("grantUriPermission success!");
120    }).catch(function (error) {
121        console.error("grantUriPermission failed with error:" + error);
122    });
123} catch (error) {
124    console.error("grantUriPermission failed with error:" + error);
125}
126  ```
127