首页 文章

使用fetch Post反映原生图像上传但面临问题?

提问于
浏览
1

我在从相机胶卷或我的API上传图片时遇到问题 . 这是我目前使用的代码 . 我能够从相机胶卷和相机中获取图像数据 . 我只是遇到了将数据发布到服务器的问题 . 我不知道问题出在哪里 . 但我认为我的formData()存在问题

同样在高级我检查我的api它的工作,如果我从邮递员或高级休息客户端上传图像,那么它是完美的工作 . 不知道为什么它不适用于本机反应 .

我正在使用的版本

“反应”:“16.0.0-beta.5”,“react-native”:“0.49.5”,“react-native-camera-kit”:“^ 5.3.14”

我将照片提交给服务器的代码

onFormSubmit =(e)=> {

var photo = {
  uri: this.state.uri[0].uri, == image path file:///storage/emulated/0/Pictures/1511787860629.jpg
  type: 'image/jpeg',
  name: 'photo.jpg',
  size: this.state.uri[0].size,==> size of image
};
var form = new FormData();
form.append("imageLink", photo);

const config = {
 method: 'POST',
 headers: {
   'Accept': 'application/json',
   'Content-Type': 'multipart/form-data',
 },
 body: form,
}

fetch("http://localhost:port/api/User/imageUpload", config)
 .then((responseData) => {
     // Log the response form the server
     // Here we get what we sent to Postman back
     console.log(responseData);
 })
 .catch(err => {
   console.log(err);
 })

}

并且api返回给我这个错误==> {“statusCode”:400,“错误”:“错误请求”,“消息”:“无效的内容类型 Headers :多部分缺失边界”}

1 回答

  • 0

    尝试使用计算机IP更改您的本地主机地址,并检查它是否有效 .

相关问题