vendredi 30 septembre 2022

File upload issue using ajax in laravel 6

I am trying to store pdf along with other input value using ajax in laravel 6. Here I did not use form html element . It shows Call to a member function getClientOriginalExtension() on null . How can I send file ?

In Blade

<input name="phone" type="text" class="form-control phone">
<input type="file" name="my_doc" accept="application/pdf" class="my_doc" id="my_doc">

<button type="button" class="store_new"> Save </button>

$(document).on('click', '.store_new', function () {
   var phone= $('.phone').val();
   var my_doc= $('.my_doc').val();

   $.ajax({
        url: '',
        type: "POST",
        headers: {
             'X-CSRF-TOKEN': ''
        },
        data: {
            phone: phone,
            my_doc: my_doc,
        },
        success: function (response) {
                   
        },
        error: function (jqXHR, textStatus, errorThrown) {

        }
     });
});

In controller

use File;

public function store(Request $request)
{
  
    $image = $request->file('my_doc');
    $new_name = rand() . '.' . $image->getClientOriginalExtension();
    $image->move(public_path('final_doc'), $new_name);

}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire