Atef95's avatar

how to pass file with the rest of attributes in post request

Hey I'm using laravel + vue

I want to pass my uploaded file with the rest of attributes in one api.. I could upload an image seperately but not within a form.

   const config = {
                        headers: {
                            'content-type': 'multipart/form-data'
                        }
                    }
                    const data = {
                        code: this.code,
                        state: this.state,
                        name: this.name,
                     }
                    let formData = new FormData();
                    formData.append('photo', this.photo);

                    axios.post('/api/products',,data,formData,config)
                        .then(function (response) {
                            if (response.data.status == 400) {
                                swal.fire({
                                    type: 'error',
                                    title: 'Code déja utilisé  !',
                                    showConfirmButton: true,
                                    confirmButtonText: 'Fermer'


                                });



                            }
                            if (response.data.status == 200) {
                                swal.fire({
                                    type: 'success',
                                    title: 'Le produit a été ajouté avec succés !',
                                    showConfirmButton: true,
                                    confirmButtonText: 'Fermer'


                                });
                                setTimeout(() => window.location = '/wizefresh/public/products', 2000);


                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });

how can I do it ?

0 likes
1 reply
ftiersch's avatar

You have to choose between normal data and formData. Just append all your data objects to your formData object and pass that with axios, that'll work :)

1 like

Please or to participate in this conversation.