Uploading file and input data using axios in larav

2019-08-04 10:42发布

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.

1条回答
贪生不怕死
2楼-- · 2019-08-04 11:24

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,
   })
查看更多
登录 后发表回答