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

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

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

HarmonyOS
9天前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
8天前
相关问题
单元测试文件如何能运行起来
2473浏览 • 1回复 待解决
request下载文件不能覆盖现有文件
1912浏览 • 1回复 待解决
HarmonyOS PDF文件保存
225浏览 • 1回复 待解决
HarmonyOS 保存图片文件异常
431浏览 • 1回复 待解决
HarmonyOS 文件下载保存问题
355浏览 • 1回复 待解决
HarmonyOS Resource的文件如何保存到沙箱
138浏览 • 1回复 待解决