【问题标题】:I want to upload an image from react-native to mongoDB using feathersJS我想使用 feathersJS 将图像从 react-native 上传到 mongoDB
【发布时间】:2019-07-15 22:49:11
【问题描述】:

我正在使用 feathersJS 在 react-native 上处理电子商务应用程序的项目我无法将图像上传到数据库中,我也在使用 react-native-blob 创建一个 blob 文件,但它不是t 工作,它抛出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))
       })
  })

【问题讨论】:

    标签: mongodb react-native feathersjs


    【解决方案1】:

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

    This 这是一篇很好的文章。

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

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2023-03-31
      • 2021-06-07
      • 2022-10-18
      • 2016-12-08
      • 2021-12-31
      相关资源
      最近更新 更多