From 7ee90bbbf754357a528d03a298dc6733188f1192 Mon Sep 17 00:00:00 2001 From: Annie_wang Date: Thu, 25 May 2023 14:54:42 +0800 Subject: [PATCH] update docs Signed-off-by: Annie_wang --- .../file-management/select-user-file.md | 120 ++++++++++++++---- 1 file changed, 96 insertions(+), 24 deletions(-) diff --git a/en/application-dev/file-management/select-user-file.md b/en/application-dev/file-management/select-user-file.md index 77fc2dd23c..d339f27e9c 100644 --- a/en/application-dev/file-management/select-user-file.md +++ b/en/application-dev/file-management/select-user-file.md @@ -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 + let uri = null; const photoPicker = new picker.PhotoViewPicker(); - photoPicker.select(photoSelectOptions) - .then(async (photoSelectResult) => { - let uri = 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}`); - }) + photoPicker.select(photoSelectOptions).then((photoSelectResult) => { + uri = photoSelectResult.photoUris[0]; + }).catch((err) => { + 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 + let file = 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 + let writeLen = 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 + let uri = null; const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance. - documentViewPicker.select(documentSelectOptions) - .then((documentSelectResult) => { - let uri = 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}`); - }) + documentViewPicker.select(documentSelectOptions).then((documentSelectResult) => { + uri = documentSelectResult[0]; + }).catch((err) => { + 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()**. + + ```ts + let config = { + action: 'ohos.want.action.OPEN_FILE', + parameters: { + startMode: 'choose', + } + } + try { + let result = await context.startAbilityForResult(config, {windowMode: 1}); + if (result.resultCode !== 0) { + console.error(`DocumentPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`); + return; + } + // Obtain the URI of the document. + let select_item_list = result.want.parameters.select_item_list; + // Obtain the name of the document. + let file_name_list = result.want.parameters.file_name_list; + } catch (err) { + 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 + let file = 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 + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + let buf = new ArrayBuffer(4096); + let num = 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 + let uri = null; const audioViewPicker = new picker.AudioViewPicker(); - audioViewPicker.select(audioSelectOptions) - .then(audioSelectResult => { - let uri = 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}`); - }) + audioViewPicker.select(audioSelectOptions).then(audioSelectResult => { + uri = audioSelectOptions[0]; + }).catch((err) => { + 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 + let file = 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 + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); ``` -- GitLab