HarmonyOS 鸿蒙Next 使用request.uploadFile上传本地相册图片失败, UploadConfig 中的files和data参数该如何定义呢?

HarmonyOS 鸿蒙Next 使用request.uploadFile上传本地相册图片失败, UploadConfig 中的files和data参数该如何定义呢?

与机型系统版本均无关。

1. 期望给出一个,上传本地相册中的图片到服务器的demo,POST请求

目前写的demo,执行完request.uploadFile后task接收不到任何返回。

static async uploadStatusImage(context: Context, uri: string) {

  // uri: file://media/Photo/73/IMG_1726626546_060/screenshot_20240918_102726.jpeg

  let cacheDir = context.cacheDir;

  let path = cacheDir + '/upload_status_image.jpeg'

  // 放到临时文件中

  if (FileUtils.exist(path)) {

    FileUtils.deleteFile(path)

  }

  console.error('image-start', path)

  let file = fs.openSync(uri, fs.OpenMode.CREATE);

  fs.copyFileSync(file.fd, path);

  fs.closeSync(file);

  // config 定义是否有问题

  let config: request.UploadConfig = {

    header: { "Content-Type": "multipart/form-data", },

    url: 'https://api.xueqiu.com/photo/upload.json',

    method: 'POST',

    files: [{

      filename: "upload_status_image.jpeg",

      name: "file",

      uri: `internal://cache${path}`,

      type: "jpeg"

    }],

    data: [{ name: "upload_status_image", value: "111" }],

  }

  console.error('image-upload', path)

  request.uploadFile(context, config, (err: BusinessError, task: request.UploadTask) => {

    if (err) {

      console.error(`image-upload() fail ${err.message}`);

      return

    }

    task.on('progress', (receivedSize: number, totalSize: number) => {

      console.error('image-2', receivedSize, totalSize)

    })

    task.on('complete', (taskStates: Array<request.TaskState>) => {

      console.error('image-1', JSON.stringify(taskStates));

    })

    task.on('headerReceive', (headers) => {

      console.error('path-4', JSON.stringify(headers));

    })

    task.on('fail', (taskStates: Array<request.TaskState>) => {

      console.error('path-3', JSON.stringify(taskStates));

    });

  })

}

更多关于HarmonyOS 鸿蒙Next 使用request.uploadFile上传本地相册图片失败, UploadConfig 中的files和data参数该如何定义呢?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
参考下这个文档,目前这个上传只能上传cache目录下的文件,如果你需要上传相册,需要利用fs文件的能力,把相册图片拷贝到cache目录下

fs参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fs-V5#fscopyfilesync

上传应用参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5#上传应用文件

参考示例

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#requestuploadfile9

更多关于HarmonyOS 鸿蒙Next 使用request.uploadFile上传本地相册图片失败, UploadConfig 中的files和data参数该如何定义呢?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,使用request.uploadFile上传本地相册图片时,UploadConfig中的filesdata参数定义如下:

  1. files参数

    • files参数用于指定要上传的文件。它是一个包含文件信息的对象数组。
    • 每个文件对象应包含filePath(文件路径)和fileName(文件名,可选,但建议提供以确保服务器正确接收)。
    • 示例:files: [{ filePath: '/storage/emulated/0/DCIM/Camera/image.jpg', fileName: 'image.jpg' }]
  2. data参数

    • data参数用于附加其他非文件数据(如用户信息、图片描述等)。
    • 它是一个键值对对象,其中键是字符串,值可以是字符串、数字或其他基本数据类型。
    • 示例:data: { userId: '12345', description: 'This is a test image' }

确保文件路径正确且应用有权限访问该路径。同时,检查服务器是否支持接收的文件类型和大小。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部