0
votes

i have a function to upload images , this upload type is json . this json like this :

"pertanyaan" => "{"question1":"Kondisi, kebersihan, pelumasan bearing","answer1":"baik","image":"gambar.png"} ◀"

on this json have a key value = image : gambar.png . this name of image is saved into json . and the problem is i cant saved this image (gambar.png) into directory file on folder . my input:

<td> <input class="form-control" style="border:none"  type="file" name="image" value="" readonly> </td>

its my controller :

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');

}

I am create a folder images at public. can someone correct my code to saved file(image) ?

1

1 Answers

2
votes

You cannot upload an image using JSON. Instead you need to use FormData objects. Once you have used FormData objects Laravel will be able to pickup the uploaded file.

Depending on how you are handling the request on your front end, you could handle this in a variety of methods.

You can find more information about FormData objects here.