尝试在vue js上传图像时未捕获的类型错误非法调用?

问题描述 投票:1回答:1

我的HTML表单是

<form method="POST" onSubmit="return false;" data-parsley-validate="true" v-on:submit="imgSubmit($event);">                                
        <h4 class="info-text">Give us somme images and videos ? </h4>
        <div class="row">  
             <div class="col-sm-6">
                <div class="form-group">
                    <label for="property-images">Chose Images :</label>
                   <input type="file" id="wizard-picture"  @change="imgSubmit($event)" multiple>
                    <p class="help-block">Select multipel images for your property .</p>
                </div>
            </div>
            <div class="col-sm-6"> 

            </div>
            <div class="wizard-footer">
             <div class="pull-right">
         <button type="submit" class='btn btn-next btn-primary' name='next' value='Next'>Next</button>
     </div>


</div>
</div></form> 

我的vue js代码是

submitBox = new Vue({
el: "#submitBox",
  data: {
pid: '',
image: '',
},
 methods: {
     imgSubmit: function(e) {
           var vm = this;
           let data = new FormData()
            data.append('name', 'image')
            data.append('file', e.target.files[0])
            $.ajax({
              url: "add/post/image/",
              data: data,
              type: "POST",
              dataType: 'json',
              success: function(e) {
              if (e.status)
              {

               alert("Registration Success")

            }
              else {
                vm.response = e;

               alert("Registration Failed") 
              }
          }
            }); 

当我使用此代码时,我收到错误

jquery-1.10.2.min.js:6 Uncaught TypeError: Illegal invocation
    at o (jquery-1.10.2.min.js:6)
    at gn (jquery-1.10.2.min.js:6)
    at Function.x.param (jquery-1.10.2.min.js:6)
    at Function.ajax (jquery-1.10.2.min.js:6)
    at Vue$3.imgSubmit (submit:883)
    at Proxy.boundFn (vue.js:167)
    at change (eval at makeFunction (vue.js:9252), <anonymous>:2:4508)
    at HTMLInputElement.invoker (vue.js:1732)

我需要上传多张图片。我怎样才能达到同样的目的?当我上传图像并在单击提交时收到以下错误时,我收到上述错误

submit:882 Uncaught TypeError: Cannot read property '0' of undefined
    at Vue$3.imgSubmit (submit:882)
    at Proxy.boundFn (vue.js:167)
    at submit (eval at makeFunction (vue.js:9252), <anonymous>:2:4120)
    at HTMLFormElement.invoker (vue.js:1732)

可以请任何人帮我实现同样的目标吗?

javascript jquery vue.js vuejs2 vue-component
1个回答
0
投票

我没有尝试使用event,但你可以实现正常功能,无需事件调用v-on:submit="imgSubmit"imgSubmit: function() {

并通过id获取文件,同时更改ajax

var imagefile = document.querySelector('#wizard-picture');
if (imagefile.files && imagefile.files[0]) {
    data.append("file", imagefile.files[0]);
}
$.ajax({
processData: false,  // tell jQuery not to process the data
contentType: false,  // tell jQuery not to set contentType
........
........

虽然你有函数eimgSubmit的变量(对于ajax)碰撞

添加您的代码段

var app = new Vue({
el: "#submitBox",
  data: {
  pid: '',
  image: '',
  },
  methods: {
    imgSubmit: function(e) {
      var vm = this;
      let data = new FormData();
      data.append('name', 'image');
      data.append('file', e.target.files[0]);
      console.log(data.get('file'));
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<div id="submitBox">
<form method="POST" onSubmit="return false;" data-parsley-validate="true" v-on:submit="imgSubmit($event);">                                
        <h4 class="info-text">Give us somme images and videos ? </h4>
        <div class="row">  
             <div class="col-sm-6">
                <div class="form-group">
                    <label for="property-images">Chose Images :</label>
                   <input type="file" id="wizard-picture"  @change="imgSubmit($event)" multiple>
                    <p class="help-block">Select multipel images for your property .</p>
                </div>
            </div>
            <div class="col-sm-6"> 

            </div>
            <div class="wizard-footer">
             <div class="pull-right">
         <button type="submit" class='btn btn-next btn-primary' name='next' value='Next'>Next</button>
     </div>


</div>
</div></form> 
</div>
© www.soinside.com 2019 - 2024. All rights reserved.