js-apis-filemanager.md 10.0 KB
Newer Older
A
annie_wangli 已提交
1 2
# Public File Access and Management
>![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
A
annie_wangli 已提交
3 4
>
>- The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
annie_wangli 已提交
5
>- The APIs of this module are system APIs and cannot be called by third-party applications. Currently, these APIs can be called only by **filepicker**.
A
annie_wangli 已提交
6 7 8
## Modules to Import

```js
A
annie_wangli 已提交
9
import filemanager from '@ohos.fileManager';
A
annie_wangli 已提交
10 11 12 13 14 15 16 17
```

## filemanager.getRoot

getRoot(options? : {dev? : DevInfo}) : Promise<FileInfo[]>

Obtains information about the files in the first-level directory in asynchronous mode. This method uses a promise to return the result.

A
annie_wangli 已提交
18 19
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
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
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
  | dev | [DevInfo](#devinfo) | No| Device name. The default value is **local**, which is the only value supported.|

- Return value

  | Type| Description|
  | --- | -- |
  | Promise<[FileInfo](#fileinfo)[]> | Promise used to return the file information obtained.|

- Example

```js
filemanager.getRoot().then((fileInfo) => {
    if(Array.isArray(fileInfo)) {
        for (var i = 0; i < fileInfo.length; i++) {
            console.log(JSON.Stringify(fileInfo))
        }
    }
}).catch((err) => {
    console.log(err)
});
```

## filemanager.getRoot

getRoot(options? : {dev? : DevInfo}, callback : AsyncCallback&lt;FileInfo[]&gt;) : void

Obtains information about the files in the first-level directory in asynchronous mode. This method uses a callback to return the result.

A
annie_wangli 已提交
51 52
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
53 54
- Parameters

A
annie_wangli 已提交
55
  | Name  | Type                     | Mandatory| Description                         |
A
annie_wangli 已提交
56
  | -------- | ------------------------- | ---- | ----------------------------- |
A
annie_wangli 已提交
57 58
  | dev      | [DevInfo](#devinfo)              | No  | Device name. The default value is **local**, which is the only value supported.|
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the file information obtained. |
A
annie_wangli 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

- Example

```js
filemanager.getRoot((err, fileInfo) => {
    if(Array.isArray(fileInfo)) {
        for (var i = 0; i < fileInfo.length; i++) {
            console.log(JSON.Stringify(fileInfo))
        }
    }
})
```

## filemanager.listFile

listFile(path : string, type : string, options? : {dev? : DevInfo, offset? : number, count? : number}) : Promise&lt;FileInfo[]&gt;

Obtains information about the files in the second-level directory in asynchronous mode. This method uses a promise to return the result.

A
annie_wangli 已提交
78 79
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
  | type | string | Yes| Type of the files to query. The file type can be **file**, **image**, **audio**, or **video**.|
  | path | string | Yes| URI of the directory to query.|
  | dev | [DevInfo](#devinfo) | Yes| Device name. The default value is **local**, which is the only value supported.|
  | offset | number | No| Start position from which the files are to query.|
  | count | number | No| Number of files to query.|

- Return value

  | Type| Description|
  | --- | -- |
  | Promise&lt;FileInfo[]&gt; | Promise used to return the file information obtained.|

- Error
A
annie_wangli 已提交
96
  | Error Info| Error Code|Description|
A
annie_wangli 已提交
97 98 99 100
  | --       | ---    | -- |
  |No such file or directory | 2      | The directory or file of the specified URI does not exist.|
  |No such process | 3 | Failed to obtain the FMS service.|
  |Not a directory | 20 | The object specified by the URI is not a directory.|
A
annie_wangli 已提交
101 102 103 104 105 106 107 108 109 110 111 112

```js
// Obtain all files in the directory.
// Call listFile() and getRoot() to obtain file URIs.
let media_path = file.uri
filemanager.listFile(media_path, "file")
.then((fileInfo) => {
    if(Array.isArray(fileInfo)) {
        for (var i = 0; i < fileInfo.length; i++) {
            console.log(JSON.Stringify(fileInfo))
        }
    }
A
annie_wangli 已提交
113
}).catch((err) => {
A
annie_wangli 已提交
114 115 116 117 118 119 120 121 122
    console.log(err)
})
```
## filemanager.listFile

listFile(path : string, type : string, options? : {dev? : DevInfo, offset? : number, count? : number}, callback : AsyncCallback&lt;FileInfo[]&gt;) : void

Obtains information about the files in the second-level directory in asynchronous mode. This method uses a callback to return the result.

A
annie_wangli 已提交
123 124
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
125 126
- Parameters

A
annie_wangli 已提交
127
  | Name  | Type                     | Mandatory| Description                                                        |
A
annie_wangli 已提交
128
  | -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
129 130
  | type     | string                    | Yes  | Type of the files to query. The file type can be **file**, **image**, **audio**, or **video**.|
  | path     | string                    | Yes  | URI of the directory to query.                                               |
A
annie_wangli 已提交
131 132 133
  | dev | [DevInfo](#devinfo) | No| Device name. The default value is **local**, which is the only value supported.|
  | offset | number | No| Start position from which the files are to query.|
  | count | number | No| Number of files to query.|
A
annie_wangli 已提交
134
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the file information obtained.                                |
A
annie_wangli 已提交
135 136
- Error

A
annie_wangli 已提交
137
  | Error Info                 | Error Code| Description                     |
A
annie_wangli 已提交
138
  | ------------------------- | ------ | ------------------------- |
A
annie_wangli 已提交
139 140 141
  |No such file or directory  | 2      | The directory or file of the specified URI does not exist.|
  |No such process            | 3      | Failed to obtain the FMS service.          |
  |Not a directory            | 20     | The object specified by the URI is not a directory.|
A
annie_wangli 已提交
142 143

```js
A
Annie_wang 已提交
144
// Call listFile() and getRoot() to obtain the file URIs.
A
annie_wangli 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
let media_path = file.uri
filemanager.listFile(media_path, "file", (err, fileInfo) => {
    if(Array.isArray(fileInfo)) {
        for (var i = 0; i < fileInfo.length; i++) {
            console.log(JSON.Stringify(fileInfo))
        }
    }
})
```

## filemanager.createFile

filemanager.createFile(path : string, filename : string, options? : {dev? : DevInfo})  :   promise&lt;string&gt;

Creates a file in the specified path in asynchronous mode. This method uses a promise to return the result.

A
annie_wangli 已提交
161 162
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
  | filename | string | Yes| Name of the file to create.|
  | path | string | Yes| URI of the file to create.|
  | dev | [DevInfo](#devinfo) | No| Device name. The default value is **local**, which is the only value supported.|

- Return value

  | Type| Description|
  | --- | -- |
  | promise<string>| Promise used to return the URI of the file created.|

- Error
A
annie_wangli 已提交
177 178 179 180 181 182
  | Error Info| Error Code|Description|
  | -- | --- | -- |
  | Operation not permitted | 1 | A file with the same name already exists.|
  | No such file or directory | 2 | The directory or file of the specified URI does not exist.|
  | No such process | 3 | Failed to obtain the FMS service.|
  | Not a directory | 20 | The object specified by the URI is not a directory.|
A
annie_wangli 已提交
183 184 185 186

```js
// Create a file.
let media_path = file.uri // Obtain the file URI using listFile() and getRoot().
A
annie_wangli 已提交
187
let name = "xxx.jpg" // File to be saved.
A
annie_wangli 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
filemanager.createFile(media_path, name)
.then((uri) => {
// The URI of the file created is returned.
})
.catch((err) => {
    console.log(err)
})
```

## filemanager.createFile

createFile(path : string, filename: string, options? : {dev? : DevInfo}, callback : AsyncCallback&lt;string&gt;) : void

Creates a file in the specified path in asynchronous mode. This method uses a callback to return the result.

A
annie_wangli 已提交
203 204
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
205 206
- Parameters

A
annie_wangli 已提交
207
  | Name  | Type                     | Mandatory| Description                         |
A
annie_wangli 已提交
208
  | -------- | ------------------------- | ---- | ----------------------------- |
A
annie_wangli 已提交
209 210
  | filename | string                    | Yes  | Name of the file to create.               |
  | path     | string                    | Yes  | URI of the file to create.            |
A
annie_wangli 已提交
211
  | dev | [DevInfo](#devinfo) | No| Device name. The default value is **local**, which is the only value supported.|
A
annie_wangli 已提交
212
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the file information obtained. |
A
annie_wangli 已提交
213 214 215

- Error

A
annie_wangli 已提交
216
  | Error Info                 | Error Code| Description                     |
A
annie_wangli 已提交
217
  | ------------------------- | ------ | ------------------------- |
A
annie_wangli 已提交
218
  | Operation not permitted   | 1      | A file with the same name already exists.             |
A
annie_wangli 已提交
219
  | No such file or directory | 2      | The directory or file of the specified URI does not exist.|
A
annie_wangli 已提交
220
  | No such process           | 3      | Failed to obtain the FMS service.          |
A
annie_wangli 已提交
221
  | Not a directory           | 20     | The object specified by the URI is not a directory.|
A
annie_wangli 已提交
222 223 224 225 226

```js
// Create a file.
// Call listFile() and getRoot() to obtain the file URI.
let media_path = file.uri
A
annie_wangli 已提交
227
// File to be saved.
A
annie_wangli 已提交
228 229 230 231 232 233 234 235 236
let name = "xxx.jpg"
filemanager.createFile(media_path, name, (err, uri) => {
// The URI of the file created is returned.
})
```

## FileInfo
Defines the file information returned by **getRoot()** or **listFile()**.

A
annie_wangli 已提交
237 238
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252
### Attributes

| Name| Type| Readable| Writable| Description|
| --- | -- | -- | -- | -- |
| name | string | Yes| No| File name.|
| path | string | Yes| No| URI of the file.|
| type | string | Yes| No| File type.|
| size | number | Yes| No| File size.|
| addedTime | number | Yes| No| Time when the file was scanned to the database.|
| modifiedTime | number | Yes| No| Time when the file was modified.|

## DevInfo
Defines the device type.

A
annie_wangli 已提交
253 254
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
255 256 257 258 259
### Attributes

  | Name| Type| Readable| Writable| Description|
  | --- | -- | -- | -- | -- |
  | name | string | Yes| Yes| Device name.|