Uploading file and input data using axios in laravel and vue

那年仲夏 提交于 2019-12-24 10:06:06

问题


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:


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,
   })


来源:https://stackoverflow.com/questions/48260651/uploading-file-and-input-data-using-axios-in-laravel-and-vue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!