dimanche 10 mai 2020

How to upload image using ajax in laravel

I have a trouble when to upload img using ajax in laravel. I have an error in getClientOriginalExtension() I think that trouble in enctype in ajax because the controller can not read the upload file.

this is my view :

<form name="data-form" id="data-form" enctype="multipart/form-data">

<input type="file" name="img_thumbnail" class="form-control">
</form>
<script type="text/javascript">
    $(function () {
        $.ajaxSetup({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
        });
       $('body').on('click', '#saveBtn', function(){
            var url;
            var registerForm = $("#data-form");
            var formData = registerForm.serialize();
            $(this).html('saving...');
            $('#saveBtn').attr('disabled',true);

            $.ajax({
                enctype: 'multipart/form-data',
                url: '',
                type:'POST',
                data:formData,
                success:function(data) {
                    console.log(data);
                    if(data.errors) {
                   }
                    if(data.success) {
                    }
                    $('#saveBtn').html('Save Data');
                    $('#saveBtn').attr('disabled',false);
                },
                error: function (data) {
                  console.log('Error:', data);
                  $('#saveBtn').html('Save Data');
              }

            });
        });
 });
    </script>

and this is my controller

$name_file = time().'.'.$request->img_thumbnail->getClientOriginalExtension();
$request->img_thumbnail->move(public_path('images'), $nama_file);


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire