Php 如何通过Axios将文件发送到Laravel
我需要通过Axios将文件从客户端发布到服务器 以下是我的Vuejs代码:Php 如何通过Axios将文件发送到Laravel,php,laravel,vue.js,vuejs2,axios,Php,Laravel,Vue.js,Vuejs2,Axios,我需要通过Axios将文件从客户端发布到服务器 以下是我的Vuejs代码: methods: { 'successUpload': function (file) { const config = { headers: { 'Content-Type': 'multipart/form-data' } }; axios.post('/Upload/File',file, config).then(function (response) {
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";
}
但它总是返回不,它不是一个文件
任何帮助都将不胜感激。您必须创建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);
});
}
}
一个例子是
让我知道这是否有效。你能告诉我你是如何调用
successUpload
的吗?我使用Dropzonejs上传文件仅供参考,但如果你使用dropzone,你不需要使用axios上传文件。请告诉我如何操作?使用url
attribute?它超出了这个问题的范围,但是如果您打开一个新的,并提供实际初始化Dropzone
的代码,我将很乐意提供帮助。