首页 > 解决方案 > 带有文件附件的表单帖子引发网络错误/React Native + react native 图像选择器

问题描述

我正在使用react-native-image-picker获取图像详细信息并尝试在 https 后端服务器中上传。请求不成功,并引发网络错误。它没有与后端服务器建立连接。问题formdata在于我发送的那个。header如果我错过了,您能否提出建议和其他信息。

  export const postImage = async state => {  

    let formData = new FormData();

    formData.append('image', {
      uri : state.photo.uri,
      type: state.photo.type,
      name : state.photo.fileName
    });


    const config = {
      headers: {
        'Content-Type': 'multipart/form-data',
        Accept: "application/x-www-form-urlencoded",
        'Accept': 'application/json'
      },
    };

    try {
    return $http.post('/image/save', formData, config)
    .then(response => response)
    .catch(error => error)
    } catch(error) {
        console.log(error)
    }
  }

环境: - Axios 版本 ^0.19.2 - 附加库版本 [React 16.11.0,React Native 0.62.1]

标签: react-nativeaxiosreact-native-image-picker

解决方案


鳍状肢存在问题,将其升级到 0.39.0 及更高版本即可

此处正在跟踪此问题:https ://github.com/facebook/react-native/issues/28551

修复:https ://github.com/facebook/flipper/issues/993#issuecomment-619823916

This should be fixed in version 0.39.0. To upgrade, edit android > gradle.properties

# Version of flipper SDK to use with React Native
FLIPPER_VERSION=0.39.0  // edit this manually

推荐阅读