HarmonyOS 选择相册图片,上传到服务器?

使用request.uploadFile上传文件,报错upOnFail taskState:{“path”:"/data/storage/el2/base/haps/entry/cache/screenShot\_1719989877434.jpg",“responseCode”:17,“message”:“Http protocol error”}

其他系统的接口调用如截图。

是因为我参数传的有问题吗?已确认是https协议

阅读 463
1 个回答

可以参考以下demo试试:

uploadImage() {
  let context1 = getContext(this) as common.UIAbilityContext;
  let uploadConfig1 = {
    url: 'http://huawei.com',
    header: {"Content-Type":"multipart/form-data","Authorization":"" },
    method: 'POST',
    files: [
      { filename: 'test.jpg', name: 'file', uri: 'internal://cache/test.jpg', type: 'jpg' }
    ],
    data: [
      { name: 'test', value: '111' }
    ]
  }
  // 将本地应用文件上传至网络服务器
  try {
    request.uploadFile(context1, uploadConfig1)
      .then((uploadTask) => {
        uploadTask.on('complete', (taskStates) => {
          for (let i = 0; i < taskStates.length; i++) {
            console.info(`xx upload complete taskState: ${JSON.stringify(taskStates[i])}`);
            this.message = JSON.stringify(taskStates[i])
          }
        });
      })
      .catch((err) => {
        console.error(`xx Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
        this.message = err.message
      })
  } catch (err) {
    console.error(`xx Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
    this.message = err.message
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏