我需要通过Axios将文件从客户端发布到服务器。
这是我的Vuejs代码:
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代码:
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
。
任何帮助都会非常感激。
答案 0 :(得分:13)
您必须创建一个FormData对象并附加图像文件。
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);
});
}
}
一个例子是here。
如果有效,请告诉我。