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

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

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%')
  }
}
  • 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.

输入日志: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优化?
967浏览 • 1回复 待解决
#鸿蒙学习大百科#什么是用户文件
1050浏览 • 0回复 待解决