[英]Problem uploading images with Axios and Laravel
I'm trying to upload and save images with Axios and a Laravel API, but i'm getting a 422 error. 我正在尝试使用Axios和Laravel API上传和保存图像,但出现422错误。
I've tested with Postman and i have the same result, i have a similar controller**(without the Foreach)** but to upload only one image at once and it works fine. 我已经用Postman进行了测试,结果也一样,我有一个类似的控制器**(没有Foreach)**,但是一次只能上传一张图片,效果很好。
///Axios
async submitFiles(){
let fd = new FormData()
for(var i = 0; i < this.files.length; i++){
let file = this.files[i]
fd.append('photo[' + i + ']', file)
}
try{
await this.$axios.$post(`/albums/${this.$route.params.id}/photos`, fd, {headers:{'Content-Type': 'multipart/form-data'}})
console.log(...fd)
alert('uploaded')
this.files = []
}
catch(err){
console.log(err)
alert(err)
}
}
//Laravel
class PhotosInAlbumController extends Controller
{
public function store(PhotoInAlbumRequest $request, Album $album)
{
if($request->hasfile('photo'))
{
$photo = new PhotoInAlbum();
$photo->photo = $request->photo;
$images[] = $request->file('photo');
foreach ($images as $image)
{
$filenameWithExt = $image->getClientOriginalName();
$filename = pathInfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $image->getClientOriginalExtension();
$filenameToStore = $filename.'_'.time().'.'.$extension;
$path = $image->storeAs('photo/images', $filenameToStore,'public');
$photo->photo = $path;
$album->photos()->save($photo);
}
}
return $photo;
}
}
Hope someone can help me to figure out what's going on. 希望有人可以帮助我弄清楚发生了什么事。
Thanks in advance (= 预先感谢(=
First, sorry I can't comment yet. 首先,对不起,我无法发表评论。 But I see in this line: $images[] = $request->file('photo'); 但是我看到这一行:$ images [] = $ request-> file('photo'); that a bi-dimensional array is built. 建立一个二维数组。 I would try the assignment without the brackets: $images = $request->file('photo'); 我会尝试不带括号的作业:$ images = $ request-> file('photo');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.