HarmonyOS filepicker在保存时如何能让它支持覆盖已有文件?

const documentSaveOptions = new picker.DocumentSaveOptions();
      documentSaveOptions.newFileNames = [result.fileName!];
      const documentViewPicker = new picker.DocumentViewPicker();
      let array = await documentViewPicker.save(documentSaveOptions)
  • 1.
  • 2.
  • 3.
  • 4.

如果文件已经存在不能选中,我的需求是存在直接覆盖就行。怎么做?

HarmonyOS
2024-12-27 15:34:58
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

当前不支持直接覆盖

readline参数是沙箱路径,无法读取文件管理路径中的文件,可以试试readSync方法,下面是demo:

import fs, { Options } from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private appContext: common.Context = getContext(this);
  private fileDir: string = ''
  @State resultFileDir: string = ''

  createFile(){
    let cacheDir = this.appContext.cacheDir;
    this.fileDir = cacheDir + '/HelloWorldlee.txt'
    let file = fs.openSync(this.fileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let writeLen = fs.writeSync(file.fd, '你好世界!');
    console.info("write data to file succeed and size is:" + writeLen);
    fs.closeSync(file)
  }

  async saveFile(){
    try {
      let documentSaveOptions = new picker.DocumentSaveOptions();
      documentSaveOptions.newFileNames = ['HelloWorld.txt'];
      let documentPicker = new picker.DocumentViewPicker();
      await documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
        let uri = documentSaveResult[0];
        this.resultFileDir = uri
        let sanFile = fs.openSync(this.fileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
        let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
        fs.copyFileSync(sanFile.fd, pubFile.fd)

        fs.close(sanFile)
        fs.close(pubFile)
        console.info('DocumentViewPicker.save successfully, documentSaveResult uri: ' + JSON.stringify(documentSaveResult));
      }).catch((err: BusinessError) => {
        console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
      });
      let pubFile = fs.openSync(this.resultFileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
      let buf = new ArrayBuffer(4096);
      fs.readSync(pubFile.fd, buf);
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
    }
  }

  build() {
    Row() {
      Column() {
        Button(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.createFile();
            this.saveFile();
          })
      }
      .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.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
分享
微博
QQ
微信
回复
2024-12-27 18:29:22
相关问题
单元测试文件如何能运行起来
3385浏览 • 1回复 待解决
request下载文件不能覆盖现有文件
2612浏览 • 1回复 待解决
HarmonyOS PDF文件保存
953浏览 • 1回复 待解决
HarmonyOS 保存图片文件异常
1347浏览 • 1回复 待解决
HarmonyOS Divider组件,如何能显示虚线
789浏览 • 1回复 待解决