#鸿蒙学习大百科#如何实现弹窗选择本地文件?

如何实现弹窗选择本地文件?

HarmonyOS
2024-10-18 09:21:23
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
小猫的老公
import picker from '@ohos.file.picker';
import { BusinessError } from '@ohos.base';
let uris: Array<string> = [];
const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例
const documentSelectOptions = new picker.DocumentSelectOptions();
documentSelectOptions.maxSelectNumber = 5; // 选择文档的最大数目(可选)
@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Text("Hello")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => {
              uris = documentSelectResult;
              console.info('documentViewPicker.select to file succeed and uris are:' + uris);
            }).catch((err: BusinessError) => {
              console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

输入日志:documentViewPicker.select to file succeed and uris are:file://docs/storage/Users/currentUser/Documents/%E6%B5%8B%E8%AF%95.docx

分享
微博
QQ
微信
回复
2024-10-18 13:43:03
相关问题
#鸿蒙学习大百科#如何实现ui优化?
229浏览 • 1回复 待解决
#鸿蒙学习大百科#什么是用户文件
222浏览 • 0回复 待解决