网络原生请求失败

时间:2018-07-04 13:15:35

标签: laravel react-native react-redux

我的本​​机应用程序遇到错误。有谁能够帮助我? 我有一个laravel代码,并创建了一个API用于在服务器上上传图像。 还有一件事,我在应用程序中使用“ react-native-file-picker”。

function addFoodImage(Request $request){

try{
if($file = $request->hasFile('image')){ 
$file =  $request->file('image'); 
                      // echo($file);
$fileName = time()."abc.jpg"; 
$destinationPath = public_path().'/images/';  
$upload =  $file->move($destinationPath,$fileName); 
$array['status']  = "success"; 
           $array['message'] =  "uploaded success";
echo json_encode($array);

       }else{  
           $array['status']  = "failed"; 
           $array['message'] = $request;
echo json_encode($array);
       }
}catch (\Exception $e) {
return $e->getMessage();
$array['status']  = "failed";
$array['message'] =  $e->getMessage();;
echo  $e->getMessage();
}
        
   }

我有一个React Native的代码来发布数据

let body = new FormData();
      body.append("image", {uri: this.state.path, filename :"imageName.jpg",type: "image/jpg"});
          body.append("Content-Type", "image/jpg");

      fetch("http://testDomain.in/apiLinks/api/uploadtest",{ method: "POST",headers:{
          "Accept": "application/json",
          "Content-Type": "multipart/form-data",
          } , body :body} )
        .then((res) => res.json())
        .then((res) => { alert("response" + JSON.stringify(res)); })
        .catch((e) => alert(e))
        .done();

此后,我收到错误消息“网络请求失败” 有谁能够帮助我? 如果您正在使用实时代码,则可以向我发送代码。如果您不习惯,请不要给我发送链接。

<Button block onPress={this.selectFileTapped.bind(this)}>
<Text>Add Photos</Text>
</Button>

class list extends Component {
selectFileTapped() {
    const options = {
      title: "File Picker",
      chooseFileButtonTitle: "Choose File..."
  };
  FilePickerManager.showFilePicker(null, (response) => {
    //alert("Response = ", response);
    if (response.didCancel) {
      Alert.alert("User cancelled file picker");
    }
    else if (response.error) {
      Alert.alert("FilePickerManager Error: ", response.error);
    }
    else {
      this.state.filename = response.fileName;
      this.state.path = response.path;
    }
  });
  }
  }

0 个答案:

没有答案