HarmonyOS axios如何上传多张图片

请问一次性上传多张图片,在上传多张图片,想问下:

1、多张图片如何传值?

2、请求参数和图片流怎么传值?

因此想知道一次性上传多张图片的解决方案

如下demo报错:Buffer is not defined code==ERR_BAD_OPTION_VALUE

//上传文件
uploadFile(buffer:ArrayBuffer,uploadUrl: string, fileName: string, fileContentType: string,
paramsMap: Map<string, string>): Promise<http.HttpResponse> {
  return new Promise<http.HttpResponse>(async (resolver, reject) => {
  try {
  let param:param = {
    aid: paramsMap.get('aid') as string,
    sid: paramsMap.get('sid')as string,
    token: paramsMap.get('token')as string,
    ext: JSON.parse(paramsMap.get('ext')as string) as object,
    ts: paramsMap.get('ts')as string,
    sign: paramsMap.get('sign')as string,
    image_files: [buffer,buffer],
    sceneId: '',
    fromType:''
  }

  let fileName = 'certImage'+systemDateTime.getTime().toString()

  let formData = new FormData()

  formData.append('file',buffer)
  formData.append('name','image_files')
  formData.append('fileName',fileName)
  axios.post<string,AxiosResponse<string>,FormData>(uploadUrl,formData,{
  method:'post',
  params:param,
  data:formData,
  headers:{'Content-Type': 'multipart/form-data'},
  context:getContext(this),
  onUploadProgress:(progress: AxiosProgressEvent): void =>{
    console.info('AxiosProgressEvent'+progress)
  }
})
.then((res:AxiosResponse)=>{
  console.info('Axiosres'+JSON.stringify(res))
}).catch((err:AxiosError)=>{
  console.info('error message='+err.message+' code=='+err.code)
})
catch (e) {
  reject(e)
}
}
})
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
HarmonyOS
2024-12-28 08:37:03
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以参考以下案例:https://blog.csdn.net/weixin_41217541/article/details/102696941

报错Buffer is not defined code==ERR_BAD_OPTION_VALUE是因为代码写错,因为

axios.post<string,AxiosResponse<string>,FormData>(uploadUrl,formData,{
  method:'post',
  params:param,
  headers:{'Content-Type': 'multipart/form-data'},
  context:getContext(this),
  onUploadProgress:(progress: AxiosProgressEvent): void =>{
    console.info('AxiosProgressEvent'+progress)
  }
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

formData的key取决于服务端,服务端定义的字段是什么就填写什么。使用append()方法时,可以通过第三个可选参数设置多部分表单数据的数据名称和数据类型,设置图片名称。

formData.append("file", "internal://cache/xx/file.txt", { filename: "text.txt", type: "text/plain"});
  • 1.
分享
微博
QQ
微信
回复
2024-12-28 09:47:36
相关问题
HarmonyOS axios如何上传图片
685浏览 • 1回复 待解决
HarmonyOS 如何使用Axios框架上传图片
931浏览 • 1回复 待解决
使用axios上传图片时候错误
760浏览 • 1回复 待解决
HarmonyOS axios上传文件失败
858浏览 • 1回复 待解决
HarmonyOS 多张图片拼接为一张
1423浏览 • 1回复 待解决
HarmonyOS web如何上传图片
535浏览 • 1回复 待解决
HarmonyOS axios上传文件数据传递问题
715浏览 • 1回复 待解决
HarmonyOS 多张string图片合并成一张
887浏览 • 1回复 待解决
HarmonyOS 多张画布横向合成一张图片
782浏览 • 1回复 待解决
HarmonyOS 图片上传
581浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
981浏览 • 1回复 待解决
HarmonyOS 图片上传失败
831浏览 • 1回复 待解决
HarmonyOS 无法上传图片
657浏览 • 1回复 待解决
HarmonyOS 上传图片问题
635浏览 • 1回复 待解决
HarmonyOS 上传图片失败
1220浏览 • 1回复 待解决
HarmonyOS 上传图片异常
719浏览 • 1回复 待解决
HarmonyOS 如何上传本地图片
835浏览 • 1回复 待解决