我的代码有什么问题。图像未在本机反应中发送上传到服务器

时间:2021-03-09 07:24:11

标签: react-native multipartform-data multipart

我是本机反应的新手。我创建了一个表单,用于将图像发送和上传到服务器。但是当上传图片时,我会得到这样的回应。我的后端在 php 中

{"type":"default","status":200,"ok":true,"headers":{"map":{"x-powered-by":"PHP/7.2.34","strict-transport-security":"max-age=63072000; includeSubDomains","expires":"Thu, 19 Nov 1981 08:52:00 GMT","date":"Tue, 09 Mar 2021 07:00:29 GMT","vary":"Accept-Encoding","x-content-type-options":"nosniff","server":"Apache","connection":"Keep-Alive","keep-alive":"timeout=3, max=500","x-frame-options":"SAMEORIGIN","content-type":"text/html; charset=UTF-8","cache-control":"public, max-age=0"}},"url":"https://nasdigital.tech/Android_API_CI/upload_multipart_data","bodyUsed":false,"_bodyInit":{"_data":{"size":1574,"offset":0,"blobId":"fab7e7ef-d28d-4aa7-b728-7055da0df5f9","__collector":{}}},"_bodyBlob":{"_data":{"size":1574,"offset":0,"blobId":"fab7e7ef-d28d-4aa7-b728-7055da0df5f9","__collector":{}}}}

并且警报显示如下=>“文件已上传”。但是在服务器数据库文件没有出现或者说图像没有得到。所以我的代码有什么问题。因为我没有收到任何警告或错误。请帮忙。

这是我的代码

 const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
     // base64: true,
    });
    if (result.cancelled) {
      return;
    }
  
    let localUri = result.uri;
    let filename = localUri.split('/').pop();
  
    let match = /\.(\w+)$/.exec(filename);
    let type = match ? `image/${match[1]}` : `image`;
  
    let formData = new FormData();
    formData.append('photo', { uri: localUri, name: filename, type });
  
    console.log(formData)
    return await fetch('https//xyxtech/Android_API_CI/upload_multipart_data', {
      method: 'POST',
      body: formData,
      // header: {
      //   'content-type': 'multipart/form-data',
      // },
      headers: {'Accept': 'application/json, text/plain, */*', 'content-type': 'multipart/form-data'},
    })
    .then((returnValue) => returnValue.json())
  // .catch(err=>err)
  .then(function(response) {
    console.log(response)
    Alert.alert("File uploaded");
    // return response.json();
  });
  };

0 个答案:

没有答案