HarmonyOS 上传图片,转化 formData 格式上传图片,转化未成功?

通过相册选取照片上传,后端解析后是一张默认图。详见图片,

通过相册选取图片的uris 转化为FormData格式上传,转化的图片与实际上传图片不一致?

demo如下

try {
  let photoSelectOptions = new picker.PhotoSelectOptions();
  photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
  photoSelectOptions.maxSelectNumber = 1;
  let photoPicker = new picker.PhotoViewPicker();
  photoPicker.select(photoSelectOptions, (err: BusinessError, photoSelectResult: picker.PhotoSelectResult) => {
    if (err) {
      console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
      return;
    }
    let uris = photoSelectResult.photoUris[0]
    console.log('PhotoViewPicker.select success: ' + uris);
    if (!uris) {
      return
    }
    let file = fs.openSync(uris,fs.OpenMode.READ_ONLY);
    let buffer = new ArrayBuffer(4096);
    let readLen = fs.readSync(file.fd, buffer);
    console.info('PhotoViewPicker data to file succeed and buffer size is:' + readLen);
    fs.closeSync(file);

    let formData = new FormData();
    formData.append('file', buffer, 'upoad.jpg');
  });
} catch (error) {
  let err: BusinessError = error as BusinessError;
  console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}

阅读 536
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏