Search code examples
strapi

how to upload image in register API in strapi?


In defaut registration API, I need to uplaod the image of user in registration API. So how could I manage it ? I'm sending in a formData and it works fine. I can see (binary) in network.

I tried to add image field and it works in admin panel but from API side I tried to send the file in key names like files, profileImage.

I didn't get the error in res. I got success in res.

Issue: When I reload the admin panel, I didn't get user's profile image.


Solution

  • Try this way. I used in react and it works fine for me.

    signUpHandler = () => {
        console.log("SignUp data ::: ", this.state);
        let data = {
            username: this.state.signUpForm.username.value,
            phone: this.state.signUpForm.phone.value,
            email: this.state.signUpForm.email.value,
            password: this.state.signUpForm.password.value
        }
        axios.post('http://0.0.0.0:1337/auth/local/register', data)
        .then(res => {
            console.log(res);
            return res.data.user.id;
        })
        .then(refId =>{
            const data = new FormData();
            data.append('files', this.state.selectedFile);
            data.append('refId', refId);            
            data.append('ref', 'user');
            data.append('source', 'users-permissions');
            data.append('field', 'profileImage');
            return axios.post('http://0.0.0.0:1337/upload', data)            
        })
        .then(res =>{
            console.log(res);
            alert("You registered successfully...");
            this.props.history.push('/login');            
        })
        .catch(error =>{
            console.log(error);
        })                
    }