3

I have been trying to upload a file with form data using axios in vue js with laravel backend. There are few solutions out there but I am not being able to make them work.

So here what I am trying to do. Here is my data in vue js and I am binding them with v-model.My form submission method and it's code

let formData = new FormData();
var file = document.querySelector('#report');
formData.append("file", file.files[0]);
formData.append('someName','someValue');

axios({
         method: 'put',
         url: self.sl+'/seller/upflv',
         data: formData,
   })

In laravel backend I am loggin using

 \Log::info($request->all());

And I get an empty array in my log file.

Here is what I can see in my chrome network

------WebKitFormBoundary0Q7B39baNw6AJXDA
Content-Disposition: form-data; name="file"; filename="d.PNG"
Content-Type: image/png
------WebKitFormBoundary0Q7B39baNw6AJXDA
someValue
------WebKitFormBoundary0Q7B39baNw6AJXDA--

Any help or explanation would be extremely helpful. Thank you.

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95

1 Answers1

11

Ran into a problem like that some time ago.

check this https://github.com/laravel/framework/issues/13457#issuecomment-239451567

Try to send it like this:

let formData = new FormData();
var file = document.querySelector('#report');
formData.append("file", file.files[0]);
formData.append('someName','someValue');
formData.append('_method', 'PUT'); // ADD THIS LINE
axios({
         method: 'post', //CHANGE TO POST
         url: self.sl+'/seller/upflv',
         data: formData,
   })
Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32
  • Is it possible to append object instead of key value pairs? Because I have already data object with all key value. – Hkm Sadek Jan 15 '18 at 10:28
  • 1
    Sure, make sure to send it as json `JSON.stringify(yourData);` and in the controller run a `json_decode()` on it. https://www.w3schools.com/js/js_json_stringify.asp , http://php.net/manual/en/function.json-decode.php – Sérgio Reis Jan 15 '18 at 10:30
  • You saved my day. @SérgioReis – S M Iftakhairul Oct 16 '20 at 17:27