0

I have in my view one input type "file", so I would like to get this file with jquery and send to my controller in Laravel, but, I don't see this file in debug

<form onSubmit="return false;" method="post" id="myForm" enctype=“multipart/form-data”>
@csrf
     <label for="input-file-max-fs">Foto do perfil</label>
     <input type="file" name="foto" id="foto" class="dropify @error('foto') is-invalid @enderror" data-max-file-size="3M" />
</form>

in the JS

$('.update).on('click', function() {
    $.ajaxSetup({
     headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     }
   });

   var dados = {
          foto: $('input[name=foto]').val(),
          .....
         }
$.ajax({
    method: 'PUT',
    dataType: 'JSON',
    url: '../perfil/editarusuario/',
    data: dados,
    success: function(res) {
        console.log(res);           
    } 
});
})

and in my Controller I trying to get the file, but no success

$file = $request->file('foto');
$file = $request->hasFile('foto')
$file = $request->file('foto')->isValid()

none of theses work

Rafael Moura
  • 1,247
  • 2
  • 14
  • 26

1 Answers1

0

You should use "new FormData()" and "contentType", here is the answer to this;

https://stackoverflow.com/a/21045034/14843602

Hasan ABLAK
  • 21
  • 1
  • 5