使用react-native-image-picker将图像上传到服务器时出错

时间:2020-09-17 09:45:29

标签: react-native axios form-data react-native-image-picker

今天,我尝试使用react-native-image-picker从手机上载图像到服务器,但是我遇到了问题。这是我的代码:

   ImagePicker.showImagePicker(options, (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);
            } else {
                var FormData = require('form-data');
                const data = new FormData();
                data.append('file', {
                    uri: `file://${response.path}`,
                    type: response.type,
                    name: response.fileName

                });
               


                Axios.post('https://api.hoc68.com/api/v1/upload_files', data, {
                    headers: {
                        'Authorization': `Bearer ${stateTree.user.token_key}`,
                        'Content-type': 'multipart/form-data',
                    }
                }).then(res => console.log(res.data)).catch(e => console.log(e.response.data))

                console.log(response.path + ' ' + response.type);
            }
        });

运行代码时,出现此错误 Possible Unhandled Promise Rejection (id: 1): TypeError: undefined is not an object (evaluating 'e.response.data')。有人可以帮助我解决此错误吗?

0 个答案:

没有答案