js-apis-filemanager.md 9.1 KB
Newer Older
A
annie_wangli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
# Public File Access and Management
>![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
>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.
## Modules to Import

```js
import filemanager from 'ohos.filemanager';
```

## System Capabilities

SystemCapability.FileManagement.FileManagerService

## 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.

- 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.

- Parameters

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

- 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.

- 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
  |  | Error Info| Error Code|Description|
  | --- | -- | --- | -- |
  |  | 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.|

```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))
        }
    }
})
.catch((err) => {
    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.

- 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) | 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.|
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes| Callback invoked to return the file information obtained.|
- Error

  |  | Error Info| Error Code| Description|
  | ------------------------- | ------------------------- | ------ | ------------------------- |
  |     | 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.|

```js
// Call listFile() and getRoot() to obtain the file UIRs.
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.

- 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
  |  | 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.|

```js
// Create a file.
let media_path = file.uri // Obtain the file URI using listFile() and getRoot().
let name = "xxx.jpg" // File name extension of the file to be saved.
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.

- 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.|
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes| Callback invoked to return the URI of the file created.|

- Error

  |  | 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.|

```js
// Create a file.
// Call listFile() and getRoot() to obtain the file URI.
let media_path = file.uri
// File name extension of file to be saved.
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()**.

### 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.

### Attributes

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