类型错误:网络请求失败,当尝试在 React Native 中上传图像时

时间:2021-05-20 06:45:50

标签: react-native

这是我的代码。但是这段代码在模拟器中运行良好,但在设备中不起作用。

selectFile = async () => {
        try {
            const res = await DocumentPicker.pick({
                type: [DocumentPicker.types.images],
            });
            console.log('res : ' + JSON.stringify(res));
            this.setState({ singleFile: res });
            this.uploadImage(res );
        } catch (err) {
            this.setState({ singleFile: null });
            if (DocumentPicker.isCancel(err)) {
                this.setState({ Msg: "Canceled" });
                this.showAlert();
            } else {
                this.setState({ Msg: "Canceled" });
                this.showAlert();
                throw err;
            }
        }
    }


uploadImage(fileToUpload) {
        // Check if any file is selected or not
        AsyncStorage.getItem('usersid', (err, ures) => {
            const baseUrl = b.abc();
            if (this.state.singleFile != null) {
                // If file selected then create FormData
                //const fileToUpload = this.state.singleFile;
                // console.log(fileToUpload.uri, 'nnnnnnnnnnnnnnnnnnn');
                // var photo = {
                //     uri: fileToUpload.uri,
                //     type: fileToUpload.type,
                //     name: fileToUpload.name,
                // };
                console.log(fileToUpload);
                let formBody = new FormData();
                formBody.append('userid', ures);
                formBody.append('profilepic', fileToUpload);
                console.log(formBody);
                // Please change file upload URL
                var url = `${baseUrl}/api/update-profile-pic`;
                fetch(url, {
                    method: 'POST',
                    headers: {
                        //'Accept': 'application/json',
                        'content-type': 'multipart/form-data;',                
                    },
                    body: formBody,
                })
                    .then(response => response.json())
                    .then((res) => {
                        console.log(res);
                        if (res.status === '1' || res.status === 1) {
                            this.setState({ isLoading: false });
                            this.setState({ Msg: res.message });
                            this.showAlert();
                        } else if (res.status === '2' || res.status === 2) {
                            this.setState({ isLoading: false });
                            this.setState({ Msg: res.message });
                            this.showAlert();
                        }
                    }).catch(err => {
                        this.setState({ isLoading: false });
                        alert(err);
                    });
            } else {
                // If no file selected the show alert
                alert('Please Select File first');
            }
        });
    };

这是我的“fileToUpload”控制台 - {"name": "IMG_20200921_165303.jpg", "type": "image/jpeg", "uri": "content://com.miui.gallery.open/raw /%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20200921_165303.jpg"}

0 个答案:

没有答案