dimanche 6 janvier 2019

can't upload image by ajax in laravel


iam try to upload image by ajax in laravel.
here is my js code :

$('#profile_picture').on('submit', function(event){
    event.preventDefault();
    $.ajax
    ({
        type: "POST",
        url: "", 
        data:new FormData(this),
        dataType:'json',
        type:'post',
        processData: false,
        contentType: false,
        cache:false,
    }).done( function(data){
      //swal("Good job!", "Your information has been successfully updated!", "success")
          console.log('Ajax was Successful!')
          console.log(data)
    }).fail(function(xhr, textStatus, error){
        console.log(textStatus)
        console.log(error)
    });
});

here is controller code :

$validation = Validator::make($request->all(), [
    'profile_photo'=> 'required|image|mimies:jpeg,png,jpg,gif|max:2048'
]);
if ($validation->passes()) {
    //$image = $request->file('profile_photo');
    $new_name = time().'.'.$request->image->getClientOriginalExtension();
    $request->image->move(public_path("photo"),$new_name);
    return response()->json([
        'message' => 'Image uploaded successfully'
    ]);
} else {
    return response()->json([
        'message' => $validation->errors()->all(),
        'profile_photo' => '',
        'class_name' => 'danger'
    ]);
}

I hope its my controller's problem . when click submit with blank , its seen error message in console.
I don't understand whats the problem ??



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2GV2ZbB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire