Javascript 使用Laravel&;Vue JS

Javascript 使用Laravel&;Vue JS,javascript,laravel,vue.js,file-upload,Javascript,Laravel,Vue.js,File Upload,所以我一直在尝试使用服务器端的Vue JS和Laravel上传多个图像文件 我的模板vue <input type="file" id = "file" ref="file" v-on:change="onImageChange" multiple /> <script> export default { data(){ return{ product: {},

所以我一直在尝试使用服务器端的Vue JSLaravel上传多个图像文件

我的模板vue

<input type="file" id = "file" ref="file" v-on:change="onImageChange" multiple />
<script>
 export default {
  data(){
    return{
      product: {},
      image: '',
    }
  },
  created() {
    let uri = `/api/product/edit/${this.$route.params.id}`;
    this.axios.get(uri).then((response) => {
        this.product = response.data;
    }); 
  },
  methods:{
    onImageChange(e){
      let files = e.target.files || e.dataTransfer.files;
      if (!files.length)
       return;
       this.createImage(files[0]);
    },
    createImage(file){
       let reader = new FileReader();
       let vm = this;
       reader.onload = (e) => {
         vm.image = e.target.result;
       };
       reader.readAsDataURL(file);
    },
    replaceByDefault(e) {
      e.target.src = this.src='/uploads/products/default_image.jpg';
    },
    saveImage(e){

        e.preventDefault()

        var file = document.getElementById('file').files;

        let formData = new FormData;
            formData.append('productId', this.product.id)
            formData.append('file', file[0])

        axios.post('/api/product/image/add', formData, {
            headers: {'Content-Type': 'multipart/form-data'}
        }).then((response) => {
            this.$router.push({name: 'view',params: { id: this.product.id }});
            });
     }

    }
   }
  </script>
let formData = new FormData;
formData.append('productId', this.product.id)
// append files
for(let i=0; i<file.length; i++){
  formData.append('file[]', file[i])
}
非常感谢大家

您可以使用
request()->file('file')
获取文件。但是,当您尝试发送文件数组时,必须在vue源中添加一些更改

Vue

<input type="file" id = "file" ref="file" v-on:change="onImageChange" multiple />
<script>
 export default {
  data(){
    return{
      product: {},
      image: '',
    }
  },
  created() {
    let uri = `/api/product/edit/${this.$route.params.id}`;
    this.axios.get(uri).then((response) => {
        this.product = response.data;
    }); 
  },
  methods:{
    onImageChange(e){
      let files = e.target.files || e.dataTransfer.files;
      if (!files.length)
       return;
       this.createImage(files[0]);
    },
    createImage(file){
       let reader = new FileReader();
       let vm = this;
       reader.onload = (e) => {
         vm.image = e.target.result;
       };
       reader.readAsDataURL(file);
    },
    replaceByDefault(e) {
      e.target.src = this.src='/uploads/products/default_image.jpg';
    },
    saveImage(e){

        e.preventDefault()

        var file = document.getElementById('file').files;

        let formData = new FormData;
            formData.append('productId', this.product.id)
            formData.append('file', file[0])

        axios.post('/api/product/image/add', formData, {
            headers: {'Content-Type': 'multipart/form-data'}
        }).then((response) => {
            this.$router.push({name: 'view',params: { id: this.product.id }});
            });
     }

    }
   }
  </script>
let formData = new FormData;
formData.append('productId', this.product.id)
// append files
for(let i=0; i<file.length; i++){
  formData.append('file[]', file[i])
}
让formData=newformdata;
formData.append('productId',this.product.id)
//附加文件
对于(i=0;i文件('file')
来获取该文件数组。但是如果您只想要其中一个(例如:第一个),您可以使用
request()->file('file.0')
来获取该文件。

您可以使用
request()->file('file'))
获取文件。但当您尝试发送文件数组时,必须在vue源中添加一些更改

Vue

<input type="file" id = "file" ref="file" v-on:change="onImageChange" multiple />
<script>
 export default {
  data(){
    return{
      product: {},
      image: '',
    }
  },
  created() {
    let uri = `/api/product/edit/${this.$route.params.id}`;
    this.axios.get(uri).then((response) => {
        this.product = response.data;
    }); 
  },
  methods:{
    onImageChange(e){
      let files = e.target.files || e.dataTransfer.files;
      if (!files.length)
       return;
       this.createImage(files[0]);
    },
    createImage(file){
       let reader = new FileReader();
       let vm = this;
       reader.onload = (e) => {
         vm.image = e.target.result;
       };
       reader.readAsDataURL(file);
    },
    replaceByDefault(e) {
      e.target.src = this.src='/uploads/products/default_image.jpg';
    },
    saveImage(e){

        e.preventDefault()

        var file = document.getElementById('file').files;

        let formData = new FormData;
            formData.append('productId', this.product.id)
            formData.append('file', file[0])

        axios.post('/api/product/image/add', formData, {
            headers: {'Content-Type': 'multipart/form-data'}
        }).then((response) => {
            this.$router.push({name: 'view',params: { id: this.product.id }});
            });
     }

    }
   }
  </script>
let formData = new FormData;
formData.append('productId', this.product.id)
// append files
for(let i=0; i<file.length; i++){
  formData.append('file[]', file[i])
}
让formData=newformdata;
formData.append('productId',this.product.id)
//附加文件
对于(让i=0;i文件('file')
来获取该文件数组。但是如果您只需要其中一个(例如:第一个),您可以使用
request()->file('file.0')
来获取该文件