2

I just updated my User model with "avatar" field so I can upload a photo. I used a PUT method already configured and just added avatar to it. In postman the file upload (form-data) it works just fine, but when trying to upload it using axios from vue.js it doesn't work. I tried in many ways, the last one, I tried to send the request as multi form data.

async saveChanges() {
      const fd = new FormData();
      fd.append("id", this.$auth.user().id);
      fd.append("username", this.$auth.user().username);
      fd.append("email", this.user.email);
      fd.append("firstName", this.$auth.user().firstName);
      fd.append("lastName", this.$auth.user().lastName);
      fd.append("isAdmin", this.$auth.user().isAdmin);
      fd.append("password", this.user.password);
      fd.append("confirmpass", this.user.confirmpass);
      fd.append("avatar", this.selectedFile, this.selectedFile.name);
      fd.append("_method", "put");
      try {
        await this.axios.put(`/users/${this.$auth.user().id}`, {
          fd
        }).then((res) => {
          console.log(res);
        });
      } catch (err) {
        console.error(err);
      }
}

After i choose the file, it is available, but I am unable to send it trough my method. Should i create another request just for updating the avatar or is it possible to solve this?

3 Answers 3

5

Pass your method as post method and define put in form data formData.append('_method', 'PUT') .

async handleSubmit() {

      let formData = new FormData();
      formData.append('image', this.file);
      formData.append('title', this.form.title);
      formData.append('_method', 'PUT')

      try {
        const response = await axios.post(`image-update/${this.$route.params.id}`, formData)

        this.$router.push('/');
        console.log(response);
      } catch (e) {
        this.error = 'Something error found !';
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

4

Try this

axios.put('/users/${this.$auth.user().id', fd, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

6 Comments

hey, tried it. still not working. I also tried to pass this trough config
You wrote that you are unable to send a request with a file. What sort of problem have you faced? Did you catch any exceptions?
no, it's not throwing any error. try { await this.axios.put(/users/${this.$auth.user().id}, fd, { headers: { "Content-Type": "multipart/form-data" } }).then((res) => { console.log(res); }); } catch (err) { console.error(err); } but i'm not getting any res in console either. in postman i am able to put with form-data, but when trying from vue js it doesn't throw any error, i m getting my file correctly, i cant figure out what am i missing
im sending the data in the body
Well, what the response do you see on the Network tab in your DevTool, when you're sending the request. What status and response object are there?
|
0

My previous wrong code that return empty result in $request->all()

let data = new FormData()
data.append('message', 'AnyMessage')

Then I change it as follows and it's working fine -

let data = "message=AnyMessage"

Comments

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.