1

I need to upload the image using vue js.

My html code to insert picture is

 <input type="file" id="wizard-picture">

My vue js code is as..

 <script>
submitBox = new Vue({
el: "#submitBox",
  data: {
   username: '',
   picture: '',

  },
  methods: {
     handelSubmit: function(e) {
           var vm = this;
           data = {};
           data['username'] = this.username;
           data['picture'] = this.picture;
            $.ajax({
              url: 'http://127.0.0.1:8000/api/add/post/',
              data: data,
              type: "POST",
              dataType: 'json',
              success: function(e) {
              if (e.status)
              {
               alert("Registration Success")

              window.location.href = "https://localhost/n2s/registersuccess.html";
            }
              else {
                vm.response = e;

               alert("Registration Failed") 
              }
          }
            });
            return false;
}
},
});
         </script>

What can I do to implement the same. This is the first time I am doing a work. I am stuck on the same. Can anybody Please help me to obtain the result?

8
  • pass e.target.files[0] to your jquery data: Commented Dec 14, 2017 at 10:32
  • can you please explain Commented Dec 14, 2017 at 11:13
  • Inside your jquery replace data:data with data:e.target.files[0] Commented Dec 14, 2017 at 11:15
  • but i also need to pass username Commented Dec 14, 2017 at 11:29
  • does it at least work for the uploading? passing the name is simple Commented Dec 14, 2017 at 11:30

1 Answer 1

1

You can try this. Just create a new formData object and pass it the name and file

Change your input to this

<input type="file" id="wizard-picture"  @change="handleSubmit($event)">

And your methods to this:

methods: {
  handleSubmit (e) {

    let data = new FormData()
    data.append('name', 'image')
    data.append('file', e.target.files[0])

    $.ajax({
      url: 'http://127.0.0.1:8000/api/add/post/',
      data: data,
      type: 'POST',
      success () {

      }else{

      }

    })

  }

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

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.