问题描述
我正在努力将图像从 vue 组件传递到 Laravel 控制器,但我只能在 Laravel 中添加一张图像。再次无法将该图像移动到图像路径。我的代码能够捕获多个图像,但我的问题是如何将它们正确传递给控制器。这是我的代码 视图
let selectedFiles=e.target.files;
if(!selectedFiles.length){
return false;
}
for(let i=0;i<selectedFiles.length;i++){
this.images=selectedFiles[i];
}
console.log(this.images);
},saveImageData(){
var self=this;
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
let formData = new FormData();
formData.append('title',this.title);
formData.append('description',this.description);
formData.append('location',this.location);
formData.append('price',this.price);
formData.append('images',this.images);
axios.post('/senddata',formData,config)
.then(function (response) {
// currentObj.success = response.data.success;
})
在 Laravel 控制器中:
public function store(Request $request)
{
$product=Products::create([
'title'=>$request['title'],'description'=>$request['description'],'price'=>$request['price'],'location'=>$request['location']
]);
$images= $request->file('images');
//dd($images->getClientOriginalName());
foreach ($images as $image){
$move=$image->move(public_path().'/images2/',$image->getClientOriginalName());
if($move){
$imagedata=Images::create([
'title'=>$image->getClientOriginalName(),'filename'=>$image->getClientOriginalName()
]);
$product->images()->attach([$imagedata->id]);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)