HarmonyOS 录音上传文件?

文件时保存在沙盒下 如:"/data/storage/el2/base/haps/entry/cache/StarWars10s-2C-48000-4SW-1719910347583.wav"如何转换成“internal://cache”+文件名然后上传

阅读 474
1 个回答

目前可以使用 fs.copyFile接口,来把文件拷贝到cache目录下再进行上传, 参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-fileio-V5\#ZH-CN\_TOPIC\_0000001930677245\_\_fileiocopyfile

上传文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5

import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs';


// 获取应用文件路径
let context = getContext(this) as common.UIAbilityContext;
let cacheDir = context.cacheDir;

@Entry
@Component
struct Index12 {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
        Button('复制').onClick(async () => {
          let uri = 'file://media/Photo/5/IMG_1718353652_004/IMG_004.jpg';
          let file = fs.openSync(uri)
          console.log('-------file', file.name)
          fs.copyFileSync(file.fd, cacheDir + "/IMG_003.jpg")
          fs.closeSync(file)
          let file1 = fs.openSync(cacheDir + "/IMG_003.jpg")
          console.log('-------file1', file1.name)
        })
      }
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏