【问题标题】:getFiles() in react native with relayjs mutationsgetFiles() 与 relayjs 突变反应本机
【发布时间】:2017-03-05 05:15:05
【问题描述】:

我需要从 react native app throw relayjs 突变下载多个文件。文件数组如下所示:

[ {uri: 'file:///path/to/file.jpg', mime: 'image/jpg'}, {uri: 'file:///path/to/file2.jpg', mime: 'image/jpg'}, ]

我发现 getFiles() func 用于突变,但我不明白它应该返回什么格式的数据?

【问题讨论】:

标签: react-native relayjs


【解决方案1】:

这就是我解决它的方法:

我使用这个包https://github.com/jeanpan/react-native-camera-roll-picker从相机胶卷中获取图像:

您可以使用它进行测试,将其添加到您的渲染中:

<CameraRollPicker
          callback={this.getSelectedImage}
          imagesPerRow={4}
          imageMargin={1}
          maximum={1}
        />

这是我的getSelectedImage的实现,我只是发送一张图片,但是你可以发送多张图片,files是一个对象,值是一张图片。

getSelectedImage = (images) => {
    const image = images[0];

    const files = {
      [image.filename]: {
        uri: image.uri,
        name: image.filename,
      },
    };

    const mutation = new UploadUserProfileMutation({
      files,
    });

    this.props.relay.commitUpdate(mutation, callbacks);
  };

在你的突变中添加方法getFiles:

export default class UploadUserProfileMutation extends Relay.Mutation {
...
  getFiles() {
    return this.props.files;
  }
...
}

你应该像正常的突变一样完成突变

【讨论】:

  • 好的,我的 getFiles() 看起来像 getFiles() { return { files: this.props.photo }; } 其中 this.props.photo = { photo0:{ uri:'file:///storage/emulated/0/DCIM/Camera/IMG_20161015_054936.jpg', name: 'IMG_20161015_054936.jpg' } } 并且我有一个错误“TypeError:网络请求失败”并且我的服务器没有看到任何请求.但是当我从我的突变中删除 getFiles func 时,一切正常。我做错了什么?(
  • 你需要在你的服务器中设置一个multer来接受文件
猜你喜欢
  • 1970-01-01
  • 2021-05-05
  • 2022-12-11
  • 2018-09-26
  • 2020-11-27
  • 2019-08-13
  • 2023-03-16
  • 2018-03-21
  • 1970-01-01
相关资源
最近更新 更多