我有一个上传图像的功能,这个上传类型是json。这个json是这样的:
"pertanyaan" => "{"question1":"Kondisi, kebersihan, pelumasan bearing","answer1":"baik","image":"gambar.png"} ◀"
在这个json上有一个关键值= image:gambar.png。这个图像名称保存到json中。问题是我无法将此图像(gambar.png)保存到文件夹上的目录文件中。我的意见:
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
它是我的控制器:
public function store(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));//this image on here
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
我在公共场合创建一个文件夹图像。有人可以更正我的代码保存文件(图像)?
您无法使用JSON上传图像。相反,您需要使用FormData对象。使用FormData对象后,Laravel将能够拾取上传的文件。
根据您在前端处理请求的方式,您可以通过各种方法处理此问题。