【问题标题】:Unable to upload image/images to node js server with expo and react native无法使用 expo 将图像/图像上传到节点 js 服务器并做出本机反应
【发布时间】:2023-01-06 22:51:24
【问题描述】:

每当我尝试从我的本机代码向服务器发送表单数据或文件时,我都没有在本机控制台中获得 assets[0].fileSize 和 assets[0].fileName。 我的 React Native 代码是:


const { status } = await Permissions.askAsync(Permissions.MEDIA_LIBRARY);
if (status !== "granted") {
  // Handle permission denied
  return;
}

const result = await ImagePicker.launchImageLibraryAsync({
  mediaTypes: ImagePicker.MediaTypeOptions.Images,
});
console.log(result);

/**
I am getting this output from result:
 {
  assets: [
    {
      assetId: "1610",
      base64: null,
      duration: null,
      exif: null,
      height: 1840,
      rotation: null,
      type: "image",
      uri: "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Fhelo-c554b96a-1355-4a71-8550-01d96488539b/ImagePicker/21bc87ec-4534-40d8-a86d-bd676ad9cbb2.jpeg",
      width: 3264,
    },
  ],
  canceled: false,
  cancelled: false,
};

**/


我正在使用 expo-image-picker 来选择图像。 在服务器端,我使用 multer().any() 来处理传入服务器的文件。

我已经尝试了更多的包:用于文件上传的 xhr 但没有用。

【问题讨论】:

    标签: javascript react-native expo


    【解决方案1】:

    Expo 返回的 URI 是对设备内存中图像数据的引用,您应该根据后端支持的数据类型上传 base64 或二进制数据。

    为了简单起见,我建议为 expo-image-picker 启用 base64 - https://docs.expo.dev/versions/latest/sdk/imagepicker/#imagepickerasset

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2020-03-04
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2011-09-12
      相关资源
      最近更新 更多