HarmonyOS 文件上传报错?

调用 request.uploadFile 的时候报错

Error: the parameters check fails   this is fail path@431a5ed1

代码如下:

uploadFile() {
  // 上传任务配置项
  let header = new Map<Object, string>();
  header.set('partner_id', this.partnerId);
  header.set('user_number', this.userNumber);
  let files: Array<request.File> = [
    { filename: 'wenzai_logs', name: 'wenzai_logs', uri: 'internal:/data/storage/el2/base/haps/entry/files/wenzai_logs.zip' , type: 'zip' }
  ]
  let requestData: Array<request.RequestData> = [{ name: 'name', value: 'value' }];
  let uploadConfig: request.UploadConfig = {
    url: API_HOST_WEB[this.deployType] + UP_LODE_LOG_FILE,
    header: header,
    method: 'POST',
    files: files,
    data: requestData
  }
  try {
    request.uploadFile(context, 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}`);
  }
}

请帮忙看下哪个地方写错了

阅读 512
1 个回答

根据文档当前能力如此,当前只支持上传cache目录下的文件, 非cache目录下上传下载解决方案: 将公共路径的文件(picker选择),拷贝至沙箱路径中,可完成上传。这边提供一个demo参考一下

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);
}

应用文件上传下载,可以参考最新更新的文档:

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏