【问题标题】:Validating using vform in Vue Js在 Vue Js 中使用 vform 进行验证
【发布时间】:2021-05-14 00:59:54
【问题描述】:

我在我的应用程序中使用 Vue Js 和 Laravel。我的代码有产品和图像。我可以使用 axios 上传图像,但是当我添加 vform 来验证我的所有图像时,没有图像被传递给控制器​​。当我将 axios.post('/senddata', formData, config) 交换为 this.form.post('/senddata', formData, config) 时,传递了其他数据,但图像为空。 这是我的代码;

             saveImageData(){
              var self=this;
              const config = {
                    headers: { 'content-type': 'multipart/form-data' }
                }
                document.getElementById('upload-file').value=[];
                let formData = new FormData();
                formData.append('title', this.form.title);
                formData.append('description', this.form.description);
                formData.append('location', this.form.location);
                formData.append('price', this.form.price);
                 for(let i=0;i<this.form.images.length;i++){
                formData.append('images[]', this.form.images[i]);
                 }
 
              axios.post('/senddata', formData, config)
                .then(function (response) {
                })
                .catch(function (error) {
                });

【问题讨论】:

    标签: laravel vue.js laravel-vue


    【解决方案1】:

    查看此文档以获取 GitHub 中的 vform 示例,该示例将指导您上传带有其他输入字段的图像:

    https://github.com/cretueusebiu/vform/blob/master/example/upload.html

    我认为这个示例方法可以解决您的问题:

        selectFile (e) {
              const file = e.target.files[0]
    
              // Do some client side validation...
    
              this.form.file = file
    
              this.form.submit('post', '/upload', {
                  // Transform form data to FormData
                  transformRequest: [function (data, headers) {
                    return objectToFormData(data)
                  }],
    
                  onUploadProgress: e => {
                    // Do whatever you want with the progress event
                    // console.log(e)
                  }
                })
                .then(({ data }) => {
                  // ...
                })
            }
    

    【讨论】:

    • 感谢 Pejman,我已经看到了,但不确定如何将数据放入 this.form.submit('post'....
    猜你喜欢
    • 2021-05-16
    • 2017-03-26
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多