0

I am trying to upload an image with vue and laravel on backend. Everything was going well until I ran into a problem. My image gets sent when i make an axios call sending formData only as an argument.

 let formData = new FormData();
 formData.append("image", this.image);
 axios.post('url',formData)

But on axios request I want to send an object, not just the formData, something like this:

const myObj={ name:'hello','shop_id':2, image:formData }
axios.post('url',myObj)

But this doesn't work. Is there any way I can work around with this?

1 Answer 1

1

Add the name and shop_id values to the FormData object. In this case FormData is the container in which you can send all of your data.

let formData = new FormData();
formData.append('image', this.image);
formData.append('name', 'hello');
formData.append('shop_id', 2);
axios.post('url',formData)

If your data becomes more complex and you need to send a big object with loads of data, then you could send the the object as a JSON value in one of the keys.

const complexObject = {
  ...loads of data here
};

formData.append('complexObject', JSON.stringify(complexObject))

Parse the JSON back to usable code on the server side to use the data inside the complex object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I feel so stupid I didn't think about this. You are a life saver :)
I've added an example how to just do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.