@@ -37,15 +37,28 @@ The **FilePicker** provides the following interfaces by file type:
Use [PhotoSelectResult](../reference/apis/js-apis-file-picker.md#photoselectresult) to return a result set. Further operations on the selected files can be performed based on the file URIs in the result set.
```ts
leturi=null;
constphotoPicker=newpicker.PhotoViewPicker();
photoPicker.select(photoSelectOptions)
.then(async(photoSelectResult)=>{
leturi=photoSelectResult.photoUris[0];
// Perform operations on the files based on the file URIs obtained.
})
.catch((err)=>{
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
console.error(`Invoke photoPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
```
5. After the GUI is returned from FilePicker, use [**fs.openSync**](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD.
```ts
letfile=fs.openSync(uri,fs.OpenMode.READ_WRITE);
console.info('file fd: '+file.fd);
```
6. Use [fs.writeSync](../reference/apis/js-apis-file-fs.md#writesync) to write data to the file based on the FD, and then close the FD.
```ts
letwriteLen=fs.writeSync(file.fd,'hello, world');
console.info('write data to file succeed and size is:'+writeLen);
fs.closeSync(file);
```
## Selecting Documents
...
...
@@ -63,23 +76,69 @@ The **FilePicker** provides the following interfaces by file type:
```
3. Create a **documentViewPicker** instance, and call [**select()**](../reference/apis/js-apis-file-picker.md#select-3) to open the **FilePicker** page for the user to select documents.
After the documents are selected successfully, a result set containing the file URIs is returned. Further operations can be performed on the documents based on the file URIs.
For example, you can use [file management APIs](../reference/apis/js-apis-file-fs.md) to obtain file attribute information, such as the file size, access time, and last modification time, based on the URI. If you need to obtain the file name, use [startAbilityForResult](../../application-dev/application-models/uiability-intra-device-interaction.md).
> **NOTE**
>
> Currently, **DocumentSelectOptions** is not configurable. By default, all types of user files are selected.
```ts
leturi=null;
constdocumentViewPicker=newpicker.DocumentViewPicker();// Create a documentViewPicker instance.
documentViewPicker.select(documentSelectOptions)
.then((documentSelectResult)=>{
leturi=documentSelectResult[0];
// Perform operations on the documents based on the file URIs.
})
.catch((err)=>{
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
```
> **NOTE**
>
> Currently, **DocumentSelectOptions** does not provide the method for obtaining the file name. To obtain the file name, use **startAbilityForResult()**.
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
}
```
4. After the GUI is returned from FilePicker, use [**fs.openSync**](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD.
```ts
letfile=fs.openSync(uri,fs.OpenMode.READ_WRITE);
console.info('file fd: '+file.fd);
```
5. Use [fs.readSync](../reference/apis/js-apis-file-fs.md#readsync) to read data from the file based on the FD, and then close the FD.
```ts
letfile=fs.openSync(uri,fs.OpenMode.READ_WRITE);
letbuf=newArrayBuffer(4096);
letnum=fs.readSync(file.fd,buf);
console.info('read data to file succeed and size is:'+num);
fs.closeSync(file);
```
## Selecting an Audio File
1. Import the **FilePicker** module.
...
...
@@ -105,13 +164,26 @@ The **FilePicker** provides the following interfaces by file type:
> Currently, **AudioSelectOptions** is not configurable. By default, all types of user files are selected.
```ts
leturi=null;
constaudioViewPicker=newpicker.AudioViewPicker();
audioViewPicker.select(audioSelectOptions)
.then(audioSelectResult=>{
leturi=audioSelectOptions[0];
// Perform operations on the audio files based on the file URIs.
})
.catch((err)=>{
console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`);
console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
```
4. After the GUI is returned from FilePicker, use [**fs.openSync**](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD.
```ts
letfile=fs.openSync(uri,fs.OpenMode.READ_WRITE);
console.info('file fd: '+file.fd);
```
5. Use [fs.writeSync](../reference/apis/js-apis-file-fs.md#writesync) to write data to the file based on the FD, and then close the FD.
```ts
letwriteLen=fs.writeSync(file.fd,'hello, world');
console.info('write data to file succeed and size is:'+writeLen);