Ajax upload image HELP

Posted 2 years ago by OlivierJazz

Hello again. I do not get away with the upload of an image in ajax. I have to forget something in the sending of the data in ajax or in the reception of these in the controller. So my form again :

{!! Form::model($user, ['route' => ['user.update', $user->id], 'method' => 'put', 'files' => true]) !!}
<div class="row content">
  @if(session()->has('ok'))
    <div class="col-lg-12">
      <div class="alert alert-success alert-dismissible">{!! session('ok') !!}</div>
    </div>
  @endif
  <div class="col-lg-10">
    <div class="panel panel-primary">
      <div class="panel-heading">Informations sur le compte
      </div>
      <div class="panel-body">
        <div class="row">
          <div class="col-sm-2 col-sm-12">
            <img src="../img/unknown.jpg" alt="..." class="img-thumbnail" id="result">
            <div class="input-file-container">
              {!! Form::file('profilImage', ['class' => 'form-control input-file', 'id' => 'file', 'name' => 'photo_profil']) !!}
              <!-- <input class="input-filegfd" id="file" type="file"> -->
              <label for="file" class="input-file-trigger" tabindex="0"><span class="ion-edit" aria-hidden="true"></span></label>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-lg-2">
    <div class="row">
      <div class="col-lg-12 col-md-6 col-sm-6">
        {!! Form::button('<span class="ion-checkmark" aria-hidden="true"></span> Valider', ['class' => 'btn btn-success content-botton', 'type' => 'submit']) !!}
      </div>
  </div>
</div>
</div>
{!! Form::close() !!}

My Ajax :

$.ajaxSetup({
       headers: {
            'X-CSRF-TOKEN': $('[name="_token"]').val()
        }
});


var file_data = $("#file").prop("files")[0];
var form_data = new FormData();
form_data.append("file", file_data)

$.ajax({
    url: '/upload',
    type: 'POST',
    data:  'form_data',
    contentType: false,
    cache: false,
    processData:false,
})
.done(function(data) {
    alert("ok");
})
.fail(function(data) {
    alert("pas bon !!!");
});

My controller :

  public function uploadImage(Request $request)
  {
    if($request->hasFile('profilImage')){

      $path = config('profil.path');

      if($imagegestion->save($request->file('profilImage'), $path)) {
        return true;
      }
    }
    die;
  }

and my route :

Route::POST('/upload', '[email protected]');

If you have any suggestions. I've been searching for several hours. Help

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.