HarmonyOS 上传图片到oss上,oss有上传的图片但请求的上传接口却没有给返回值?

请求的传参:

let requestMultipartJson: http.MultiFormData[] = [{
  "name": "key",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "policy",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "callback",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "signature",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "host",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "OSSAccessKeyId",
  "contentType": "",
  "data": "xxxx"
}, {
  "name": "success_action_status",
  "contentType": "",
  "data": 200
}, {
  "name": "file",
  "contentType": "image/jpeg",
  "filePath": "/data/storage/el2/base/haps/entry/filesIMG_20240620_143754.jpg",
  "filename": "IMG_20240620_143754.jpg"
}]

通过这种方式传参之后,后端oss那边收到了图片,后端那边看也正常返回了,但是打印出来的返回数据中result是空对象,没有收到返回值。经排查是解析响应方式的问题,有其他的方式解析响应吗?请求代码如下:

let result = await http.createHttp().request(ImgObjs.uploadUrl, {
  method: http.RequestMethod.POST,
  header: {
    'Content-Type': 'multipart/form-data',
    'Connection': 'keep-alive'
  },
  expectDataType: http.HttpDataType.ARRAY_BUFFER,
  multiFormDataList: requestMultipartJson,
}
)
hilog.info(0xFF00, "上传结果", JSON.stringify(result));
console.log('上传结果' + JSON.stringify(result))
}
catch(err) {
  console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`);
}
阅读 458
1 个回答

指定数据返回的类型是ARRAY\_BUFFER,可以使用toJSON和toString方法转换。

logo
HarmonyOS
子站问答
访问
宣传栏