简体   繁体   English

错误:网络错误 Axios - Image React Native

[英]Error: Network Error Axios - Image React Native

Im trying to post an Image to server but axios throw an error saying我试图将图像发布到服务器但 axios 抛出错误说

[Error: Network Error] [错误:网络错误]

I created a bodyFormData and appended the following我创建了一个 bodyFormData 并附加了以下内容

bodyFormData.append('image', {
          name: imgName,
          type: 'image/jpeg',
          uri: this.state.image,
        });

where my imageName is just a string and the this.state.image is the file path file:///storage/emulated/0/Pictures/0cccb0f1-375b-44b8-b15b-4625536a8d63.jpg其中我的imageName只是一个字符串,而this.state.image是文件路径file:///storage/emulated/0/Pictures/0cccb0f1-375b-44b8-b15b-4625536a8d63.jpg

Axios Call Axios 调用

const addProduct = async product => {
  try {
    var prod = await axios({
      method: 'post',
      url: baseURL + 'addProduct',
      data: product,
      headers: {
    'content-type': `multipart/form-data; boundary=${product._boundary}`,
    Accept: 'application/json',
  },
      },
    });
    return prod;
  } catch (err) {
    throw err;
  }
};

after alot of trials i understand that in bodyFormData even i write any kind of json object经过大量试验后,我了解到在bodyFormData中,即使我写了任何类型的 json object

const oj = {
          name: 'a',
          c: 8,
        }

bodyFormData.append('image', obj  );

it throws me the same error它给我带来了同样的错误

[Error: Network Error] [错误:网络错误]

Kindly please drop the best possible solution for the problem请放弃该问题的最佳解决方案

Thank you for your precious time.感谢您的宝贵时间。

I recently implemented this (working) code我最近实现了这个(工作)代码

const formData = new FormData();

formData.append('file', {
  type: 'image/jpeg',
  name: `myname.jpeg`,
  uri: isAndroid() ? uri : uri.replace('file://', ''),
});

and with this header in the api call并在 api 调用中使用此 header

'Content-Type': 'multipart/form-data'

It's very similar to your code - maybe the uri part then?它与您的代码非常相似-也许是uri部分?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native Axios 上传图片返回 Network Error on Android - React Native Axios upload image return Network Error on Android 在 React Native 中使用 axios 时出现网络错误 - Network Error when use axios in React Native Axios POST 请求在 React Native 中将图像添加到 FormData 结构时给出“网络错误” - Axios POST request gives a “Network Error” when adding image to FormData structure in React Native React Native https axios resquest 在 Android 上出现网络错误,但在 iOS 上没有 - React Native https axios resquest gets Network Error on Android but not iOS 使用 Axios 或在 android 中获取时反应本机抛出网络错误 - React Native throw Network Error when using Axios or Fetch in android 在 Android 物理设备上反应本机 axios 后请求网络错误 - React native axios post request network error on Android physical device 错误:在 React Native 中使用 axios 的网络错误,仅在 iOS 和 Android 中出现 - Error: Network Error using axios in React Native, issue in only in iOS and Android React-native - Axios GET请求在Android模拟器中引发网络错误 - React-native - Axios GET request throws network error in Android emulator 使用 axios 访问在 localhost 上运行的 api 时,React Native 应用程序 GET 请求导致网络错误 - React Native app GET request resulting in network error when using axios to reach for api running on localhost 使用 Axios 响应本机 POST 请求:即使在使用服务器的 IP 后出现网络错误 - React Native POST request with Axios: Network Error even after using Server's IP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM