#鸿蒙学习大百科#如何实现文件的拷贝并监听拷贝进度?

如何实现文件的拷贝并监听拷贝进度?

HarmonyOS
2024-10-16 08:41:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
莫名瞄一眼
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import fileUri from '@ohos.file.fileuri';
@Entry
@Component
struct Index {
  @State message: string = '12312 121'
  private context = getContext(this) as common.UIAbilityContext;
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            try {
              let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
                console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
              };
              let copyoption: fs.CopyOptions = {
                "progressListener": progressListener
              }
              let applicationContext = this.context.getApplicationContext();
              let cacheDir = applicationContext.cacheDir;
              //将aaa文件拷贝到bbb文件,如果bbb不存在系统会自动创建一个bbb文件
              fs.copy( fileUri.getUriFromPath(cacheDir + "/aaa"),  fileUri.getUriFromPath(cacheDir + "/bbb"), copyoption).then(() => {

              })
            } catch (err) {
              console.error(`Failed to copy: ${JSON.stringify(err)}`);
            }

          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-10-16 16:40:27
相关问题
HarmonyOS选取拷贝文件失败
282浏览 • 1回复 待解决
#鸿蒙学习大百科#如何实现ui优化?
136浏览 • 1回复 待解决
ArkTS中如何实现对象拷贝
166浏览 • 1回复 待解决