0

I'm trying to upload image using vue.js in Laravel for that i'm using this link

https://jsfiddle.net/b412ruzo/

to upload image using vue.js and when i submit the form i'm getting following image in files array

enter image description here

now issue is that i cannot get this file array in laravel controller

when i print

$request->file('files') 

in my controller i am getting null. and when i print $request->input('files') this is the result, an empty array

enter image description here

Any help regarding this issue is highly appreciated.

Code Snippet :

data() {
     return {
    rawData: [],
    formData: new Form({
        files:[],
})
..

  const header = {
             Authorization: "Bearer " + this.token,
            };
  this.formData
        .post(APP_URL + `/api/post`, { headers: header })
        .then((response) => {

   }
4
  • add your ajax code where your sending file Commented Oct 23, 2020 at 10:07
  • @KamleshPaul: Added code snippet, Please check, i can retrieve other fields value but issue is for only image Commented Oct 23, 2020 at 10:14
  • why your not using axios .? Commented Oct 23, 2020 at 10:18
  • @KamleshPaul: it was old code and previously image was uploaded with plugin, but now i want to upload it with custom function. Commented Oct 23, 2020 at 10:23

1 Answer 1

1

not sure you can send ajax request via this.formData.post

try this

new Vue({
  el: "#app",
  data() {
    return {
      option: {
        maxFileCount: 3
      },
      files:[],
      rawData: [],
    }
  },
  methods: {
    loaddropfile: function(e) {
        e.preventDefault()
      e.stopPropagation()
        alert('ok')
        console.log(e)
    },
    openinput: function() {
        document.getElementById("vue-file-upload-input").click();
    },
    addImage: function(e) {
        const tmpFiles = e.target.files
      if (tmpFiles.length === 0) {
        return false;
      }
      const file = tmpFiles[0]
      this.files.push(file)
      const self = this
        const reader = new FileReader()
      reader.onload = function(e) {
        self.rawData.push(e.target.result)
      }
      reader.readAsDataURL(file)
    },
    removeFile: function(index) {
        this.files.splice(index, 1)
      this.rawData.splice(index, 1)
      document.getElementById("vue-file-upload-input").value = null
    },
    upload: function() {
        alert('Check console to see uploads')
        console.log(this.files)
      axios.post(`${APP_URL}/api/post`,{files:this.files},{ headers: header })
        .then((response) => {});

    }
  },
  mounted(){

  }
})

it will send your form data to files key so you can get all the files via $request->file('files')

6
  • I'm getting message: "Unauthenticated." 401 after using axios Commented Oct 23, 2020 at 10:28
  • I have valid token {Authorization: "Bearer Gx6j7U4No1P73jKawvV0KjXIFRiYG8eoSJsZVtBcksA…meIMTD5XBaIpCooIA9C8RBsEOsjTBLlrGhQp04m7nKNelnAHy"} when i remove axios it works Commented Oct 23, 2020 at 10:41
  • tried with axios also getting null in dd($request->file('files')); Commented Oct 23, 2020 at 11:02
  • try dd($request->all()); Commented Oct 23, 2020 at 11:03
  • Added output it is returning same result for file field but getting other fields Is there any issue in Vue.js code that is provided in the above fiddle link Commented Oct 23, 2020 at 11:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.