> hvigor ERROR: Failed :entry:default@PreviewArkTS... > hvigor ERROR: ArkTS:ERROR File: D:/Exploit/Item/uploadSTH/entry/src/main/ets/pages/User/UserEdit.ets:169:12 Property 'on' does not exist on type 'void & Promise<UploadTask>'.

这个错误表明在 task 对象(类型为 void & Promise<UploadTask> )上不存在 on 方法。

可能的原因是您所使用的 request.uploadFile 返回的对象不支持 on('headerReceive', callback) 这种用法。

您可以检查相关的文档或 API 说明,确认正确的处理上传任务的响应方式。也许应该使用 then 方法来处理成功的响应,使用 catch 方法来处理错误。

import { request } from '@kit.BasicServicesKit';
const task = await request.uploadFile(context, {
        url: 'https://hmajax.itheima.net/api/uploadimg',
        header: {
          'content-type': 'multipart/form-data'
        },
        method: http.RequestMethod.POST,
        data: [],
        files: [{
          filename: imgName,
          name: 'img',
          uri: `internal://cache/${imgName}`,
          type: imgName?.split('.').pop()
        }]
      })
      task.on('headerReceive', (res: object) => {
        const IRes = res as IUploadRes
        const imgUrl = JSON.parse(IRes.body) as IBody
        //AlertDialog.show({ message: imgUrl.url })
        this.userInfo.avatar = imgUrl.data.url
      })
    })

导入的包与实际的包不一样,需要从新把包导入才能用,或者用之前那个包的写法直接用 .then()

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐