HarmonyOS使用axios进行相册图片上传操作失败

使用axios进行图片上传失败 报错提示 Error: empty error message, 不知道是什么原因导致的。代码:

const photoViewPicker = new picker.PhotoViewPicker(); 
let ctx = getContext(this) 
let photoSelectResult = await photoViewPicker.select(photoSelectOptions) 
uris = photoSelectResult.photoUris; 
for (let index = 0; index < uris.length; index++) { 
  const element = uris[index]; 
  console.error('====>uri is:' + element + ', start read file') 
  fs.open(element, (err, file) => { 
    if (err) { 
      console.error("====>file open failed with error message: " + err.message + ", error code: " + 
      err.code); 
    } else { 
      let filepath = `${ctx.cacheDir}/${file.name}`; 
      console.error('====>filepath is:' + filepath + ', start copy file') 
      fs.copyFile(file.fd, filepath, () => { 
        // 上传的uri 
        let uri = fileUri.getUriFromPath(filepath) 
        console.error("文件uri路径:" + uri) 
        let realUri = `internal://cache/${file.name}`; 
        console.error("real uri:" + realUri) 
        // 执行上传 
        let request: AxiosRequestConfig<FormData> = { 
          url: config.subUrl, 
          headers: { 
            'Content-Type': 'multipart/form-data' 
          }, 
          context: getContext(), 
          method: "post", 
          params: params, 
          data: formData 
        } 
        let resp: AxiosResponse<HttpResponseModel> = 
          await axios.request<HttpResponseModel, AxiosResponse<HttpResponseModel>, FormData>(request) 
      }); 
    } 
  }) 
} 
console.error('photoViewPicker.select to file succeed and uris are:' + uris); 
} catch (err) { 
  console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
}

错误提示:

E     [ecmascript] Pending exception before ToObject called in line:239, exception details as follows: 
Error: empty error message 
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1) 
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1) 
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1) 
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1) 
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15) 
[ecmascript] Pending exception before Has called in line:1891, exception details as follows: 
Error: empty error message 
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1) 
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1) 
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1) 
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1) 
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15) 
[ecmascript] Pending exception before IsMixedDebugEnabled called in line:3573, exception details as follows: 
Error: empty error message 
at upload (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/upload.js:140:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:31:1) 
at anonymous (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/adapters/ohos/index.js:26:1) 
at dispatchRequest (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/dispatchRequest.js:51:1) 
at request (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/core/Axios.js:170:1) 
at wrap (oh_modules/.ohpm/@ohos+axios@2.2.0/oh_modules/@ohos/axios/src/main/ets/components/lib/helpers/bind.js:5:1) 
at apiRequest (entry/src/main/ets/core/network/service.ets:90:15) 
[default] ConstructorOptimize:2520 occur exception need return
HarmonyOS
2024-09-12 13:08:43
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

在请求中未设置请求头,headers: { ‘Content-Type’: ‘multipart/form-data’ },请参考gitee的代码实例:https://gitee.com/openharmony-sig/ohos_axios#%E5%BD%93%E4%B8%8A%E4%BC%A0%E7%9A%84%E5%86%85%E5%AE%B9%E4%B8%BAarraybuffer%E6%97%B6%E7%94%A8%E6%B3%95%E5%A6%82%E4%B8%8B

formData里有其他字段会触发empty error message问题,只包含file这一个字段可以请求成功:

formData.append(‘file’, internalUri)
分享
微博
QQ
微信
回复
2024-09-12 17:16:04
相关问题
使用axios上传图片时候错误
98浏览 • 1回复 待解决
HarmonyOS如何上传图片到模拟器相册
802浏览 • 1回复 待解决
拉起相册选择图片页面失败
1880浏览 • 1回复 待解决
通过web上传图片进行预览
905浏览 • 1回复 待解决
基于PhotoViewPicker对图片进行操作
760浏览 • 1回复 待解决
如何对相册图片进行编辑裁剪
1736浏览 • 1回复 待解决
使用FilePick上传uri失败,报错201
1696浏览 • 1回复 待解决
HarmonyOS 测试命令行相使用
432浏览 • 1回复 待解决
从本机获取图片进行上传到spring后端
2355浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
410浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
373浏览 • 1回复 待解决
HarmonyOS web上传文件和图片
200浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
398浏览 • 1回复 待解决
HarmonyOS 保存图片相册问题
223浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
166浏览 • 1回复 待解决