From 3935ba56573aad034484fd92c7e688a1e92e0b83 Mon Sep 17 00:00:00 2001 From: huweiqi Date: Fri, 19 May 2023 03:14:07 +0000 Subject: [PATCH] filePicker add Perform subsequent operations on the uri Signed-off-by: huweiqi Change-Id: I7e6b01a96880d316a66e9f9306b0f6cd9a65c36b --- .../file-management/save-user-file.md | 88 ++++++++++++++----- .../file-management/select-user-file.md | 88 ++++++++++++++----- 2 files changed, 128 insertions(+), 48 deletions(-) diff --git a/zh-cn/application-dev/file-management/save-user-file.md b/zh-cn/application-dev/file-management/save-user-file.md index 4695c62d48..e82454a2f8 100644 --- a/zh-cn/application-dev/file-management/save-user-file.md +++ b/zh-cn/application-dev/file-management/save-user-file.md @@ -24,15 +24,28 @@ 用户选择目标文件夹,用户选择与文件类型相对应的文件夹,即可完成文件保存操作。保存成功后,返回保存文档的URI。 ```ts + let uri; const photoViewPicker = new picker.PhotoViewPicker(); - photoViewPicker.save(photoSaveOptions) - .then(async (photoSaveResult) => { - let uri = photoSaveResult[0]; - // 获取到到图片或者视频文件的URI后进行文件读取等操作 - }) - .catch((err) => { - console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`); - }) + photoViewPicker.save(photoSaveOptions).then((photoSaveResult) => { + uri = photoSaveResult[0]; + }).catch((err) => { + console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`); + }) + ``` + +4. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +5. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭fd。 + + ```ts + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); ``` ## 保存文档类文件 @@ -58,15 +71,28 @@ > 目前DocumentSelectOptions不支持参数配置,默认可以选择所有类型的用户文件。 ```ts + let uri; const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例 - documentViewPicker.save(documentSaveOptions) - .then(async (documentSaveResult) => { - let uri = documentSaveResult[0]; - // 例如,可以根据获取的URI进行文件写入等操作 - }) - .catch((err) => { - console.error(`Invoke documentPicker.save failed, code is ${err.code}, message is ${err.message}`); - }) + documentViewPicker.save(documentSaveOptions).then((documentSaveResult) => { + uri = documentSaveResult[0]; + }).catch((err) => { + console.error(`Invoke documentPicker.save failed, code is ${err.code}, message is ${err.message}`); + }) + ``` + +4. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +5. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭fd。 + + ```ts + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); ``` ## 保存音频类文件 @@ -91,13 +117,27 @@ > 目前AudioSelectOptions不支持参数配置,默认可以选择所有类型的用户文件。 ```ts + let uri; const audioViewPicker = new picker.AudioViewPicker(); - audioViewPicker.save(audioSaveOptions) - .then((audioSelectResult) => { - let uri = audioSelectResult[0]; - // 获取到到音频文件的URI后进行文件读取等操作 - }) - .catch((err) => { - console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`); - }) + audioViewPicker.save(audioSaveOptions).then((audioSelectResult) => { + uri = audioSelectResult[0]; + }).catch((err) => { + console.error(`Invoke audioPicker.select failed, code is ${err.code}, message is ${err.message}`); + }) + ``` + +4. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +5. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭fd。 + + ```ts + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); ``` + \ No newline at end of file diff --git a/zh-cn/application-dev/file-management/select-user-file.md b/zh-cn/application-dev/file-management/select-user-file.md index bcd5e82e10..c706f8a36d 100644 --- a/zh-cn/application-dev/file-management/select-user-file.md +++ b/zh-cn/application-dev/file-management/select-user-file.md @@ -37,15 +37,28 @@ 文件选择成功后,返回[PhotoSelectResult](../reference/apis/js-apis-file-picker.md#photoselectresult)结果集,可以根据结果集中URI进行文件读取等操作。 ```ts + let uri; const photoPicker = new picker.PhotoViewPicker(); - photoPicker.select(photoSelectOptions) - .then(async (photoSelectResult) => { - let uri = photoSelectResult.photoUris[0]; - // 获取到图片或者视频文件的URI后进行文件读取等操作 - }) - .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. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +6. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭fd。 + + ```ts + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); ``` ## 选择文档类文件 @@ -73,15 +86,13 @@ > 目前DocumentSelectOptions不支持参数配置,默认可以选择所有类型的用户文件。 ```ts + let uri; const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例 - documentViewPicker.select(documentSelectOptions) - .then((documentSelectResult) => { - let uri = documentSelectResult[0]; - // 获取到文档文件的URI后进行文件读取等操作 - }) - .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}`); + }) ``` > **说明:** @@ -110,6 +121,22 @@ } ``` +4. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +5. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭fd。 + + ```ts + let writeLen = fs.writeSync(file.fd, 'hello, world'); + console.info('write data to file succeed and size is:' + writeLen); + fs.closeSync(file); + ``` + + ## 选择音频类文件 1. 导入选择器模块。 @@ -135,13 +162,26 @@ > 目前AudioSelectOptions不支持参数配置,默认可以选择所有类型的用户文件。 ```ts + let uri; const audioViewPicker = new picker.AudioViewPicker(); - audioViewPicker.select(audioSelectOptions) - .then(audioSelectResult => { - let uri = audioSelectOptions[0]; - // 获取到音频文件的URI后进行文件读取等操作 - }) - .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. 待界面从FilePicker返回后再使用文件管理的接口[fs.openSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#fsopensync)根据目的文件的uri打开这个文件得到fd。 + + ```ts + let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); + console.info('file fd: ' + file.fd); + ``` + +5. 通过fd使用[fs.writeSync](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-file-fs.md#writesync)接口对这个文件进行编辑修改,编辑修改完成后关闭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