我想使用 feathersJS 将图像从 react-native 上传到 mongoDB

I want to upload an image from react-native to mongoDB using feathersJS

我正在使用 feathersJS 处理 react-native 上的电子商务应用程序项目我无法将图像上传到数据库中,我也在使用 react-native-blob 创建一个 blob 文件但是它不工作,它抛出 FileReader.readAsArrayBuffer is not implemented 的错误。 我只想上传一张图片,然后取回它,请帮助我使用 react-native。

https://github.com/feathersjs/feathers/issues/348

uploadImageFromDevice = () => {
    const options = {
      title: 'Select a Photo',
      cancelButtonTitle: 'Cancel',
      takePhotoButtonTitle: 'Take Photo…',
      chooseFromLibraryButtonTitle: 'Choose from Library…'
    };
    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', response);
      if (response.didCancel) {
        console.log('User cancelled image picker');
      } else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      } else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      } else {
        { this.storeImageToFeathers(response, response.fileName); }
      }
    });
  }

storeImageToFeathers = (response, mime = ';BASE64') => new Promise((resolve, reject) => {
    const uri = response.uri;
    const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
    const name = new Date().toString();
    const imageRef = firebase.storage().ref('/photos/posts').child(name);
    fs.readFile(uploadUri, 'base64')
      .then(data => Blob.build(data, { type: `${mime};BASE64` }))
      .then((blob) => {
        console.log(blob);
        client.service('image-upload').create({
          uri: blob
        }).then((res) => console.log(res))
          .catch(err => console.log(err.message))
       })
  })

要将文件上传到 feathersjs,您应该使用多部分表单。您必须安装使用 Express 中间件的模块 multer 来获取上传的文件。

This 这是一篇很好的文章。

您的方法存在问题,您上传的文件是 string64 格式。这有一个大问题,当文件有点大时,请求可能会因为大小而失败。多部分形式分批上传文件。