jeudi 9 juin 2022

Can not access a image file in controller using jquery in laravel

I am trying to catch all data to controller using jquery in laravel. Except image file, i caught all the data in controller and i tried lots of code but did not success. In the below i gave controller and view code of laravel.

View:

<form action="" method="post" enctype="multipart/form-data" id="forms">
@csrf
<div class="row">
    <div class="col-md-4"> 
        <label>Profile Picture</label>
        <input class="form-control" type="file" name="file" id="file">
    </div>
</div>
<div>
    <button type="submit" id="submit">Submit</button>
</div>
</form>
<script>
    $(function(){        
    $("#forms").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url:$(this).attr('action'),
            method:$(this).attr('method'),
            data:new FormData(this),
            processData:false,
            dataType:'json',
            contentType:false,
            success:function(data){
                console.log(data);
                
            }
        });
    });
});
</script>

Controller:

public function store(Request $request){
    return $request->all();  
}

and also i tried this code:

public function store(Request $request){
    return $request->file('file');  
}

i failed to catch the image file in my controller using javascript. please help if anyone can. Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire