HarmonyOS 上传图片失败?

用request.uploadfile()上传文件时,报the parameters check fails Parameter verification failed, GetInternalPath failed, filePath is not valid,上传的路径如图所示

阅读 527
1 个回答

request.uploadFile不支持file协议路径,要把文件拷贝到沙箱目录,传递沙箱路径

目前只支持沙箱路径中cache缓存目录上传下载。 非cache目录下上传下载解决方案: 将公共路径的文件(picker选择),拷贝至沙箱路径中,可完成上传。

Demo:

pickerPhoto() {
  try {
    let options = new picker.PhotoSelectOptions();
    options.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
    options.maxSelectNumber = 1;
    let p = new picker.PhotoViewPicker();
    p.select(options).then((res: picker.PhotoSelectResult) => {
      console.info("tag photo is uri" + res.photoUris[0])
      this.saveFile(res.photoUris[0]);
    })
  } catch (e) {
    console.error("tag select tailed..")
  }
}

async saveFile(uri:string){
  // 将公共目录的文件,拷贝至沙箱路径
  let destDist = getContext(this).cacheDir + "/test.png";
  let src = fs.openSync(uri, fs.OpenMode.READ_ONLY);
  let dst = fs.openSync(destDist, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  fs.copyFileSync(src.fd, dst.fd);
}
logo
HarmonyOS
子站问答
访问
宣传栏