【问题标题】:React Native file upload with Rails API使用 Rails API 进行 React Native 文件上传
【发布时间】:2017-08-12 04:18:26
【问题描述】:

我正在使用带有 Rails 作为 API 的 react native 构建移动应用程序,我在上传图像时遇到问题 我使用 react-native-image-picker 库在 IOS 中上传相机胶卷图像,使用 XMLHttRequest 连接到 API 端点在 Rails 中上传文件时,我使用了默认的载波设置。

当我尝试从邮递员上传时,它正在工作,但从本地反应却没有。以下是我在响应本机上传请求中的代码:

ImagePicker.launchImageLibrary(options, (response) => {
      if (response.didCancel) {
        console.log('User cancelled photo picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        var source;

        // You can display the image using either:
        //source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};

        //Or:
        if (Platform.OS === 'android') {
          source = {uri: response.uri, isStatic: true};
        } else {
          source = {uri: response.uri.replace('file://', ''), isStatic: true};
        }
        var photo = {
          uri: source.uri,
          type: 'image/jpeg',
          name: response.fileName
        }
        var xhr = new XMLHttpRequest();

        var body = new FormData();
        body.append('authToken', token);
        body.append('avatar', photo);

        xhr.open('POST', uri);

        xhr.send(body)

【问题讨论】:

    标签: ruby-on-rails api react-native


    【解决方案1】:

    我解决了我的问题,我使用了 fetch 而不是 XMLHttpRequest 和

    【讨论】:

    • 我忘记了标题中的授权
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2023-01-03
    • 2017-03-08
    • 2019-04-28
    • 2015-08-07
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多