7

I have a data Image which store as a Blob but I dont know how to post it with Axios, I use VUEJS. Please help me.

My Object API by VueDevtool enter image description here

<file-upload v-model="files"></file-upload>
<button type="submit" v-on:click.prevent="Submit">Submit</button>

<script>
  methods: {
    data: function () {
      return {
        config: {
          'headers': {'Authorization': 'JWT ' + this.$store.state.token},
          'Content-Type': 'multipart/form-data'
        }
    },
    methods:{
      for (var file in this.files) {
        let data = new FormData()
        data.append('image', this.file[0])
        data.append('caption', 'image')
        data.append('user', this.Authuser)
        api.post('/photos/create/', data, this.config)
      }
    }
  }
</script>
1
  • @RishiRaut no. Image which store as a Blob, bro Commented Dec 14, 2017 at 9:05

1 Answer 1

14

You are almost there. The only thing you need is to append the actual file and you should pass $event to your function as: Submit($event)

    Submit(event) {
      let URL = '....'
    
      let data = new FormData()
    
      data.append('name', 'image')
      data.append('file', event.target.files[0])
      
      let config = {
        header : {
          'Content-Type' : 'multipart/form-data'
        }
      }
    
      axios.post(URL, data, config).then(response => {
        console.log('response', response)
      }).catch(error => {
        console.log('error', error)
      })
    }
Sign up to request clarification or add additional context in comments.

6 Comments

I got this error: Uncaught TypeError: Cannot read property '0' of undefined
Ah, sorry that is supposed to be in your input, but since you are using a file upload? component then maybe use the model this.files[0]
If i make methods like this, it came to error: Cannot read property 'append' of undefined
Why do you need <file-upload> component? You can upload with it
which part of this code convert image to blob?
|

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.