无法将图像上传到PHP服务器React Native

时间:2020-10-27 14:42:04

标签: react-native axios image-upload

今天,我做了一个将图像上传到PHP服务器的函数,现在它在IOS上可以正常工作,但是当我在android模拟器上运行时,它并不成功。 这是我的代码:

PHP服务器:

<?php

$data = array('status'=>false);

if(isset($_POST['submit'])){
    $target_file = "upload/". basename($_FILES['file']['name']);
    $file_type = pathinfo($target_file, PATHINFO_EXTENSION);
    $is_image = getimagesize($_FILES['file']['tmp_name']);
    if($is_image){
        $data['image'] = "upload/". time() . '.' . $file_type;
        if(move_uploaded_file($_FILES['file']['tmp_name'], $data['image'])){
            $data['status'] = true;
        }else {
            $data['message'] = 'Error on uploading image';
        }
    }else {
        $data['message'] = 'File is not an image';
    }
}

header('Access-Control-Allow-Origin: *');
header('Content-type:application/json');

echo json_encode($data);

反应本机代码:

       ImagePicker.showImagePicker(options, (response) => {
        console.log('Response = ', response);
        if (response.didCancel) {
            console.log('User cancelled image picker');
        } else if (response.error) {
            console.log('ImagePicker Error: ', response.error);
        } else if (response.customButton) {
            console.log('User tapped custom button: ', response.customButton);
            alert(response.customButton);
        } else {
            SetFileuri(response.uri) //update state to update Image
            console.log(response)
            Alert.alert(
                'Confirm',
                'Are you sure to update your avatar?',
                [

                    {
                        text: 'No',
                        onPress: () => SetFileuri(null),
                        style: 'cancel'
                    },
                    {
                        text: 'OK', onPress: () => {
                            const formData = new FormData();
                            formData.append('submit', 'ok');
                            formData.append('file', { type: response.type, uri: Platform.OS == 'ios' ? response.uri : 'file://' + response.path, name: "noname.jpg" });
                            console.log(formData);
                            setTimeout(() => {
                                axios.post('https://svhutech.nonamee.com/upload.php', formData, {
                                    headers: {
                                        'Content-Type': 'multipart/form-data',
                                    }
                                })
                                    .then((res) => {
                                        console.log(res);
                                    }).catch((err) => {
                                        console.log(err)
                                    })
                            }, 1000);
                        }
                    }
                ],
                { cancelable: false }
            );
        }
    });

我尝试过:

  • 将android:usesCleartextTraffic =“ true”添加到AndroidManifest.xml中
  • 添加
  • 使用访存代替axios 我已经搜索并引用了一些相同的案例,但是仍然没有成功,任何人都可以帮忙!非常感谢<3

0 个答案:

没有答案