HarmonyOS documentViewPicker.select获取到的本地路径,fs.statSync\(path\)调用会报错,找不到文件?

按照文档,本地测试:(使用fd和path会闪退)

import picker from '@ohos.file.picker';
import { BusinessError } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';

@Entry
@Component
struct DocPickerPage {
  @State message: string = 'hello World';
  @State uri: Array<string> = []

  async selectFile() {
  try {
    let DocumentSelectOptions = new picker.DocumentSelectOptions();
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => {
      this.uri = DocumentSelectResult
    }).catch((err: BusinessError) => {
    });
  } catch (error) {
  }
}

  build() {
    Row() {
      Column() {
        Text('选择文件')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.selectFile()
          })

        // select返回的uri权限是只读权限,开发者可以根据结果集中uri做进一步的处理。
        // 注意不能在picker的回调里直接使用此uri进行打开文件操作,需要定义一个全局变量保存uri,使用类似一个按钮去触发打开文件。
        Text('获取文件信息')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            let path: string = this.uri[0]
            let file = fs.openSync(path, fs.OpenMode.READ_ONLY);
            let fileFd = 'fd://' + file.fd
            let stat1 = fs.statSync(file.fd);
            console.log(`result ==> ${JSON.stringify(stat1)}`);
            let stat2 = fs.statSync(fileFd);
            console.log(`result ==> ${JSON.stringify(stat2)}`);
            let stat3 = fs.statSync(path);
            console.log(`result ==> ${JSON.stringify(stat3)}`);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
阅读 382
1 个回答

请参考以下demo:

import picker from '@ohos.file.picker';
import { BusinessError } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';

@Entry
@Component
struct DocPickerPage {
  @State uri: Array<string> = []

  async selectFile() {
    try {
      let DocumentSelectOptions = new picker.DocumentSelectOptions();
      let documentPicker = new picker.DocumentViewPicker();
      DocumentSelectOptions.maxSelectNumber = 0;
      documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => {
        this.uri = DocumentSelectResult
        // 文件具体信息
        this.getFileInfo()
      }).catch((err: BusinessError) => {
      });
    } catch (error) {
    }
  }

  getFileInfo() {
    let path: string = this.uri[0]
    let file = fs.openSync(path, fs.OpenMode.READ_ONLY);
    let stat = fs.statSync(file.fd);

    console.log(`result ino ==> ${stat.ino}`);
    console.log(`result mode ==> ${stat.mode}`);
    console.log(`result uid ==> ${stat.uid}`);
    console.log(`result gid ==> ${stat.gid}`);
    console.log(`result size ==> ${stat.size}`);
    console.log(`result atime ==> ${stat.atime}`);
    console.log(`result mtime ==> ${stat.mtime}`);
    console.log(`result ctime ==> ${stat.ctime}`);
    console.log(`result location ==> ${stat.location}`);
  }

  build() {
    Row() {
      Column() {
        Text('选择文件')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.selectFile()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏