Alizey's avatar

Single Image Upload Using Ajax

i want to know how can i upload an image with ajax. Without ajax image uploading works fine. but how we can use ajax to upload image. Ajax

$.ajax({
          url: '{{ Url::To("/profile/ajax") }}',
          type: "post",
          data: {'filename':$('#input-24').val(), '_token': $('input[name=_token]').val()},
          success: function(data){
             $('#modal-header-primary').modal('hide');
          },
           complete: function(){
            $('#modal-header-primary').modal('hide');
          }
        });  


And Controller

if(Request::ajax()) {
        $data = Request::get('filename');
        if(Request::hasFile('filename'))
        {
            $profile = Profile::Where('userid','=',Auth::user()->id)->first();
            $file = Request::file('filename');
            $image_name = time()."-".$file->getClientOriginalName();
            $file->move('uploads', $image_name);
            $image = Image::make(sprintf('uploads/%s', $image_name))->resize(350, 350)->save();
            $profile->profilePic = $image_name;
            $profile->save();
            return $profile->profilePic;
        }
    }
0 likes
3 replies

Please or to participate in this conversation.