在Vue.js和Laravel中上载多个图像
我试图在vue.js和laravel中同时上传多个文件,我使用的代码是: Vue.js代码: 我得到了这个错误:在Vue.js和Laravel中上载多个图像,laravel,vue.js,laravel-5,Laravel,Vue.js,Laravel 5,我试图在vue.js和laravel中同时上传多个文件,我使用的代码是: Vue.js代码: 我得到了这个错误: 在我试图获取图像的地方,有两个问题,评论中提到的一个 另外一个我在你的vue代码中注意到,你每次都用文件内容覆盖变量this.user.degree_attachment,所以首先它不会多次上传,其次它的类型不会是数组,所以laravel控制器不知道如何处理,因此为foreach()提供的参数无效这仅仅是因为它不是迭代对象类型 我没有在laravel部分尝试,我在这个控制台中测试了v
在我试图获取图像的地方,有两个问题,评论中提到的一个 另外一个我在你的vue代码中注意到,你每次都用文件内容覆盖变量
this.user.degree_attachment
,所以首先它不会多次上传,其次它的类型不会是数组,所以laravel控制器不知道如何处理,因此为foreach()提供的参数无效
这仅仅是因为它不是迭代对象类型
我没有在laravel部分尝试,我在这个控制台中测试了vue,并将每个文件的内容作为数组项记录到数组中,但这通常是由laravel验证触发的
uploadDegree(e) {
let a = [];
for (let file of e.target.files) {
try {
let reader = new FileReader();
reader.onloadend = (file) => {
a.push(reader.result);
};
reader.readAsDataURL(file);
this.degree_attachment = a;
} catch {}
}
console.log(this.degree_attachment);
},
$files=$request->file('degree_attachment')
notinput()
它是文件
@KamleshPaul:已尝试,但仍然收到与base64相同的错误发送?它应该是this.user.degree\u attachment.push(reader.result)
然后它将是数组,您可以循环over@KamleshPaul:我已经尝试了您的代码,但它仍然为我提供了为foreach()提供的无效参数。
尝试了为foreach()提供的无效参数。
返回或转储$file,其类型是什么?难道它不适合吗??
$files = $request->input('degree_attachment');
foreach($files as $file) {
$degreename = time() . '.' . explode('/', explode(':', substr($file, 0, strpos($file, ';')))[1])[1];
$pathDegreeExist = public_path("img/degree_img/");
if(!File::exists($pathDegreeExist)) File::makeDirectory($pathDegreeExist, 777);
$img = \Image::make($file);
$img->save(public_path('img/degree_img/').$degreename);
$newUser->degree_attachment = $degreename;
}
Invalid argument supplied for foreach()
uploadDegree(e) {
let a = [];
for (let file of e.target.files) {
try {
let reader = new FileReader();
reader.onloadend = (file) => {
a.push(reader.result);
};
reader.readAsDataURL(file);
this.degree_attachment = a;
} catch {}
}
console.log(this.degree_attachment);
},