2017-12-14 4 views
1

vue jsを使用して画像をアップロードする必要があります。画像を挿入するvue jsを使用して画像をアップロードするにはどうしたらいいですか?

私のhtmlコードは

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

マイVUEのjsのコードのようにある..です

<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> 

私は同じことを実現するために何ができますか。私が仕事をしているのは初めてです。私は同じことに固執しています。誰でも結果を得るのを助けてくれますか?

+0

パス 'e.target.files 0:ちょうどこれにこの

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

そしてあなたの方法にあなたの入力を変更し

新しいFORMDATAオブジェクトを作成し、名前を渡し、

を提出] 'あなたのjquery' data: ' – samayo

+0

あなたは説明してください – med

+0

あなたのjqueryの中で' data:e.target.files [0] ' – samayo

答えて

0

これを試すことができます。 [

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{ 

     } 

    }) 

    } 

} 
関連する問題