HarmonyOS 文件上传无法解析文件地址

//将文件拷贝入沙箱中
copyFileToSandBox() : string[]{
  let files : string[] = [];
  let cachDir = this.context.cacheDir;
  this.imgArr.forEach((uri) => {
    let file = fs.openSync(uri,fs.OpenMode.READ_ONLY);
    let name = file.name;
    let dest = fs.openSync(cachDir + "/" + name,fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    fs.copyFileSync(file.fd,dest.fd);
    fs.closeSync(file.fd);
    let destUri = dest.path;
    fs.closeSync(dest.fd);
    files.push(destUri);
  })
  return files;
}
//将文件上传服务器
uploadPics(){
  if (this.imgArr && this.imgArr.length > 0 ) {
    let fileUris = this.copyFileToSandBox();
    let files: Array<request.File> = []
    for (let i = 0; i < fileUris.length; i++) {
      let filename = fileUris[i].substring(fileUris[i].lastIndexOf("/") + 1);
      let name = filename.substring(0,filename.indexOf("."));
      let fileType = filename.substring(filename.indexOf(".") + 1);
      console.log("filename=" + filename + ";name=" + name + "type:" + fileType);
      files.push({
        filename: filename,
        name: name,
        uri: "internal:/" + fileUris[i],
        type: fileType
      })
    };
    let header = new Map<Object, string>();
    header.set("Content-type","application/x-www-form-urlencoded")
    let data : Array<request.RequestData> = [{name:"serviceCode",value:"handheld_bus_file"},{name:"twoServiceCode",value:"feedback"}];
    let uploadConfig: request.UploadConfig = {
      url: UrlConst.ROOT_URL + UrlConst.FILE_UPLOAD_URL,
      header: header,
      method: 'POST',
      files: files,
      data: data
    }
    try {
      request.uploadFile(getContext(this), uploadConfig)
        .then((uploadTask: request.UploadTask) => {
          uploadTask.on('complete', (taskStates: Array<request.TaskState>) => {
            for (let i = 0; i < taskStates.length; i++) {
              console.info(`upload complete taskState: ${JSON.stringify(taskStates[i])}`);
            }
          });
        })
        .catch((err: BusinessError) => {
          console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
        })
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
    }
  }
}

上述代码中this.imgArr为从相册中选择的图片的uri数组,执行该代码后报错

Invoke uploadFile failed, code is 401, message is the parameters check fails Parameter verification failed, GetInternalPath failed, filePath is not valid

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

1.header.set(“Content-type”,“multipart/form-data”)

2.uri格式 uri: “internal:/” + ‘/cache/’+filename,

详见https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#file

分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS上传文件问题
617浏览 • 1回复 待解决
HarmonyOS 如何上传文件
154浏览 • 1回复 待解决
HarmonyOS 地址解析问题
52浏览 • 1回复 待解决
HarmonyOS 录音上传文件
104浏览 • 1回复 待解决
HarmonyOS 上传文件问题
87浏览 • 1回复 待解决
HarmonyOS 如何解析证书文件
450浏览 • 1回复 待解决
HarmonyOS 上传文件相关Demo
555浏览 • 1回复 待解决
HarmonyOS 文件上传报错
93浏览 • 1回复 待解决
HarmonyOS web组件上传文件
349浏览 • 1回复 待解决
HarmonyOS文件上传怎么写
569浏览 • 1回复 待解决
HarmonyOS 上传文件的demo
21浏览 • 1回复 待解决
HarmonyOS 文件管理查找pdf文件上传
61浏览 • 1回复 待解决
HarmonyOS 如何解析xml文件
74浏览 • 1回复 待解决
HarmonyOS日志文件上传问题
601浏览 • 1回复 待解决
HarmonyOS 文件上传 & 资源读取问题
594浏览 • 1回复 待解决
HarmonyOS web上传文件和图片
290浏览 • 1回复 待解决
HarmonyOS 文件上传入参设置
68浏览 • 1回复 待解决