HarmonyOS 通过documentViewPicker.select选择的文件调用fs.copyFileSync报错?

const chooseFile = (params: ESObject): Promise<ESObject> => {
  return new Promise((resolve, reject) => {
    const documentSelectOptions = new picker.DocumentSelectOptions()
    documentSelectOptions.maxSelectNumber = 0;
    const documentViewPicker = new picker.DocumentViewPicker();
    documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => {
      const uri: string = documentSelectResult[0];
      const fileName = getFileName(uri);
      const fileType = fileName.lastIndexOf('.') > -1 ? fileName.substring(fileName.lastIndexOf('.')) : '';
      const localId = `${dayjs().format('YYYYMMDDHHmmssSSS')}_${Global.getInstance().getPartIMEI()}${fileType}`;
      const saveDir = `${SYSTEM_DIRECTORY_DMTMP}files/`;
      const dest = `${saveDir}${localId}`;
      if (!isFileExist(saveDir)) {
        fs.mkdirSync(saveDir, true);
      }
      // 拷贝文件到临时目录
      fs.copyFileSync(uri, dest);
      resolve({
        localId: localId,
        fileName: fileName,
        fileType: fileType,
        localPath: dest,
        fileSize: getFileSize(dest)
      });
    }).catch((err: BusinessError) => {
      reject(err);
    })
  })
}

通过documentViewPicker.select选择的文件调用fs.copyFileSync报错

阅读 530
1 个回答

需要检查一下文件路径是否正确

fs.copyFileSync示例参考

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

@Entry
@Component
struct CopyPhotoPage {
  @State selectUris: Array<string> = [];
  @State saveUris: Array<string> = [];

  build() {
    Column() {
      Text('点击测试').onClick(()=>{
        this.getPhotoUrl()
      })
    }
  }

  getPhotoUrl() {
    try {
      let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 1;
      let photoPicker = new photoAccessHelper.PhotoViewPicker();
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
        this.selectUris = PhotoSelectResult.photoUris;
        console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
        this.getSaveUrl()
      }).catch((err: BusinessError) => {
        console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
    }
  }

  async getSaveUrl() {
    try {
      const photoSaveOptions = new picker.PhotoSaveOptions(); // 创建文件管理器保存选项实例
      photoSaveOptions.newFileNames = ["PhotoViewPicker01.png"]; // 保存文件名(可选),方括号里的文件名自定义,每次不能重复,设备里已有这个文件的话,名字就需要改个不一样的,不然接口会报错
      const photoViewPicker = new picker.PhotoViewPicker();
      try {
        let photoSaveResult = await photoViewPicker.save(photoSaveOptions);
        if (photoSaveResult != undefined) {
          this.saveUris = photoSaveResult;
          console.info('photoViewPicker.save to file succeed and uris are:' + photoSaveResult);
          this.copy()
        }
      } catch (error) {
        let err: BusinessError = error as BusinessError;
        console.error(`[picker] Invoke photoViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
      }
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.info("[picker] photoViewPickerSave error = " + JSON.stringify(err));
    }
  }

  copy() {
    try {
      let photoSelect = fs.openSync(this.selectUris[0], fs.OpenMode.READ_ONLY);
      let photoSave = fs.openSync(this.saveUris[0], fs.OpenMode.WRITE_ONLY);
      console.info("photoSelect:",photoSelect)
      console.info("photoSave:",photoSave)
      fs.copyFileSync(photoSelect.fd, photoSave.fd);
      fs.close(photoSelect);
      fs.close(photoSave);
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.info("[picker] Photo Save error = " + JSON.stringify(err) + err.message);
    }
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏