在Android上的React Native中使用Axios上传图像

时间:2020-08-03 05:01:10

标签: react-native axios react-native-android

我正在尝试使用axios上传文件,请求失败。已经搜索了很多,似乎每个人都在做同样的事情并且为他们工作。有什么我想念的吗?

我的传奇请求如下所示:

function* postUploadUtilityList(list) {
  console.log('List', list.data);
  const { data } = list;
  for (const i in data) {
    if (list.data.hasOwnProperty(i)) {
      yield call(postUploadUtility, data[i]);
    }
  }
}

const createFormData = (photo, body) => {
  const data = new FormData();
  data.append('image', photo);

  Object.keys(body).forEach((key) => {
    data.append(key, body[key]);
  });

  return data;
};

function* postUploadUtility(item) {
  console.log('item', item);
  try {
    const body = {
      caption: 'utility',
    };
    const formData = createFormData(item, body);
    const apiConfig = {
      method: 'post',
      baseURL: getBaseUrl(BB),
      url: '/client/upload',
      data: formData,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'multipart/form-data',
      },
    };
    const res = yield call(http, apiConfig);
    if (res.status === 200) {
      const { data } = res;
      yield put({
        type: POST_UTILITY_UPLOAD_SUCCESS,
        data,
      });
    } else {
      const { data } = res;
      yield put({
        type: POST_UTILITY_UPLOAD_FAILURE,
        data,
      });
    }
  } catch (e) {
    yield put({
      type: POST_UTILITY_UPLOAD_FAILURE,
      e,
    });
  }
}

export default function* watchPostUploadUtility() {
  yield takeLatest(POST_UTILITY_UPLOAD, postUploadUtilityList);
}

日志如下所示: enter image description here

网络通话如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

尝试此解决方案,使用图像 base64 代替图像url