• 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, flag: wantConstant.Flags, 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| flag   | wantConstant.Flags | 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**Example**
48
49  ```js
50import wantConstant from '@ohos.app.ability.wantConstant';
51
52
53let uri = 'datashare:///media/image/8';
54let bundleName = 'com.demo.test';
55try {
56    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err) => {
57        if (err) {
58            console.error("grantUriPermission failed with error: " + err);
59            return;
60        }
61        console.info("grantUriPermission success!");
62    });
63} catch (error) {
64    console.error("grantUriPermission failed with error:" + error);
65}
66  ```
67
68## fileShare.grantUriPermission
69
70grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise&lt;void&gt;
71
72Grants permissions on a user file by the URI to an application. This API uses a promise to return the result.
73
74**Required permissions**: ohos.permission.WRITE_MEDIA
75
76**System API**: This is a system API.
77
78**System capability**: SystemCapability.FileManagement.AppFileService
79
80**Parameters**
81
82| Name| Type  | Mandatory| Description                      |
83| ------ | ------ | ---- | -------------------------- |
84| uri   | string | Yes  | URI of a user file.|
85| bundleName   | string | Yes  | Bundle name of the application to be grated with the permissions.|
86| flag   | wantConstant.Flags | 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.|
87
88**Return value**
89
90  | Type                          | Description        |
91  | ---------------------------- | ---------- |
92  | Promise&lt;void&gt; | Promise that returns no value.|
93
94**Error codes**
95
96For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
97
98| ID                    | Error Message       |
99| ---------------------------- | ---------- |
100| 201 | Permission verification failed |
101| 202 | The caller is not a system application |
102| 401 | The input parameter is invalid |
103| 143000001 | IPC error |
104
105**Example**
106
107  ```js
108import wantConstant from '@ohos.app.ability.wantConstant';
109
110let uri = 'datashare:///media/image/8';
111let bundleName = 'com.demo.test';
112try {
113    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
114      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(function () {
115        console.info("grantUriPermission success!");
116    }).catch(function (error) {
117        console.error("grantUriPermission failed with error:" + error);
118    });
119} catch (error) {
120    console.error("grantUriPermission failed with error:" + error);
121}
122  ```
123