[ASK] How to upload local image phone to server

 const formData = new FormData();
                formData.append('file', this.state.image);
                formData.append('username', this.state.username);
                formData.append('email', this.state.email);
                formData.append('full_name', this.state.name);
                formData.append('phone_number', this.state.phoneNumber);
                formData.append('birth', this.state.birth);
                formData.append('gender', this.state.checked);

alert(JSON.stringify(formData));
                
        
                fetch( this.state.APIUrl + '/users/edit-profile/' + this.state.token , {
                    method: 'POST', // or 'PUT'
                    body: formData, // data can be `string` or {object}!
                        headers: {
                            Accept: 'application/json',
                            'Content-Type': 'multipart/form-data',
                        }
                }).then(responseEdit => responseEdit.json())
                .then( responseEdit => {
                    //alert('Berhasil update profile !');
                    //this.props.navigation.navigate('HomeUser');
                })
                //.then(response => console.log('Success:', JSON.stringify(response)))
                .catch(error => {
                   console.log('Gagal terhubung ke server !');
                }); 
these my function to pick image from gallery and camera using expo imagePicker:
btnGalleryClicked = async () => {
        await this.askPermissionsAsync();
        let result = await ImagePicker.launchImageLibraryAsync(
            {
                allowsEditing: true,
                aspect: [4,3] ,
            }
        );
        if(!result.cancelled){
            this.setState({ image: result.uri, isVisible:false});
        }
    };


btnCameraClicked = async () => {
        await this.askPermissionsAsync();
        let result = await ImagePicker.launchCameraAsync(
            {
                allowsEditing: true,
                aspect: [4,3] ,
            }
        );
        
        if(!result.cancelled){
            this.setState({ image: result.uri, isVisible:false });
        }
    };

this is my ouput formData() =>

{“_parts”:[[“file”,“file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540wahidin32%252Fangkumobile/ImagePicker/b99ba701-bd47-49e2-844d-8cb48166aecc.png”],[“username”,“???”],[“email”,“???@gmail.com”],[“full_name”,“??? Rainnx”],[“phone_number”,“”],[“birth”,“18/01/1997”],[“gender”,1]]}

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.