如何通过 Axios 将文件发送到 Laravel [英] How to send a file via Axios to Laravel
本文介绍了如何通过 Axios 将文件发送到 Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过 Axios 将文件从客户端发布到服务器.
I need to post a File from client to server via Axios.
这是我的 Vuejs 代码:
Here is my Vuejs code :
methods: {
'successUpload': function (file) {
const config = { headers: { 'Content-Type': 'multipart/form-data' } };
axios.post('/Upload/File',file, config).then(function (response) {
console.log(response.data);
});
}
}
这是我处理发送文件的 Laravel 代码:
And here is my Laravel code for handling sent file :
public function uploadFile(Request $request)
{
if($request->hasFile('file'))
return "It's a File";
return "No! It's not a File";
}
但它总是返回No It's not a File
.
任何帮助将不胜感激.
推荐答案
您必须创建一个 FormData 对象并附加图像文件.
You have to create a FormData object and append the image file.
methods: {
'successUpload': function (file) {
let data = new FormData();
data.append('file', document.getElementById('file').files[0]);
axios.post('/Upload/File',data).then(function (response) {
console.log(response.data);
});
}
}
一个例子是这里.
让我知道这是否有效.
这篇关于如何通过 Axios 将文件发送到 Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文