Vue.js与Laravel 8 API图像上载引发500错误
下面是此代码的图像上传路径;这是我的模板上传代码Vue.js与Laravel 8 API图像上载引发500错误,laravel,vue.js,laravel-8,image-upload,http-status-code-500,Laravel,Vue.js,Laravel 8,Image Upload,Http Status Code 500,下面是此代码的图像上传路径;这是我的模板上传代码 <div class="form-group"> <div class="form-row"> <div class="col-md-6"> <input @change="onFileSelected" class="custom-file-input" t
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<input @change="onFileSelected" class="custom-file-input" type="file">
<small class="text-danger" v-if="errors.photo"> {{errors.photo[0]}} </small>
<label accept="image/*" class="custom-file-label" for="customFile" id="photo">Choose file</label>
</div>
<div class="col-md-6">
<img :src="form.photo" style="height: 40px; width:40px;">
</div>
</div>
</div>
以下是我推送日期的代码
employeeinsert()
{
console.log(this.form.photo)
axios.post('/api/employee',this.form)
.然后(()=>{
//console.log(this.form)
//这是.$router.push({name:'employee'})
//烤面包,火({
//图标:“成功”,
//标题:“员工添加成功”
// })
})
.catch(error=>this.errors=error.response.data.errors)
}
这些是Laravel 8后端验证。在这里,我尝试获取请求图像和名称更改,并尝试保存我的公用文件夹
if($request->photo){
$position=strpos($request->photo,“;”);
$sub=substr($request->photo,0,$position);
$ext=分解('/',$sub)[1];
$name=time().“..$ext;
$img=Image::make($request->photo)->调整大小(240200);
$upload_path='backend/employee';
$inage\u url=$upload\u path.$name;
$img->save($inage\u url);
返回console.log($inage_url);
雇员::插入([
“名称”=>$request->name,
“电子邮件”=>$request->email,
“地址”=>$request->address,
“薪资”=>$request->salary,
'加入日期'=>$request->加入日期,
'nid'=>$request->nid,
“电话”=>$request->phone,
“照片”=>$last\u图像
]);
当我上传一个图像时,我得到一个500错误
下面是所有日志的500错误
我不知道它为什么抛出500。请帮助解决这个问题。在代码中可以看到的导致500错误的事情很少有
返回console.log($inage\u url)
:console.log()是javascript而不是PHP
$inage\u url=$upload\u path.$name;
应该是$inage\u url=$upload\u path./'.$name
否则$img->save($inage\u url);可能会导致错误,因为路径格式不正确
你可以检查一下你的错误日志,看看它为什么会抛出500个内部服务器错误不知道为什么是500,但我可以确保我的db和其他配置是正确的。在你的代码中,可能导致500个错误的几件事是-1:
return console.log($inage\u url)
console.log是javascript而不是php,2:$inage\u url=$upload\u path.$name;
它应该是$inage\u url=$upload\u path.'/'.$name,否则$img->save($inage\u url)
可能会导致错误,因为路径格式不正确,3:不清楚$last_图像在'photo'=>$last_图像中来自何处accept@Anonymousshooter完成-添加作为回答,以方便后续访问者
ata() {
return {
form:{
name: null,
email: null,
address: null,
salary: null,
joining_date: null,
nid: null,
phone: null,
photo: null
},
errors: {
}
}
},
methods: {
onFileSelected(event){
// console.log(event)
let file = event.target.files[0];
if(file.size > 1048770) {
// Notification.image_validation()
Toast.fire({
icon: 'error',
title: 'upload image less than 1MB'
})
}else {
//console.log(form)
let reader = new FileReader();
reader.onload = event =>{
this.form.photo = event.target.result
console.log(event.target.result)
};
reader.readAsDataURL(file)
}
},