Categories

Saturday, July 20, 2024
#919814419350 therichposts@gmail.com
Laravel

Upload file in laravel with ajax jquery

Laravel 7.2 routing with route group auth guard check with prefix

Upload file in laravel with ajax jquery

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to Upload file in laravel with ajax jquery? Like I always say that laravel is the best Mvc php framework.

Laravel file upload with jquery ajax formdata.

Here is this post, we will upload file in laravel with ajax and here is working and tested code:
//View file code:
<div>
    <input type="file" id="fileupload" name="photos[]" data-url="/upload" multiple />
    <br />
    <div id="files_list"></div>
    <p id="loading"></p>
    <input type="hidden" name="file_ids" id="file_ids" value="" />                    
 </div>

//jscode

$(document).ready(function(){ 
    $("input").change(function(){

      var uploader = $('#image-uploader[type="file"]');
      var data = new FormData();
      $.each(uploader.files, function() {
        data.append('image[]', this);
      });
      data.append('_token', $('[name="csrf-token"]').attr('content'));
      var url = '/upload'; //Or any target path with post method
      $.ajax({
        url: url,
        method: 'POST',
        data: data,
        processData: false,
        contentType: false,
        success: function(data) {
            alert('succeed');
        }
    });

 });
});

//Route File:
Route::post('/upload', 'UploadController@uploadSubmit');

//Controller File Code:
 public function uploadSubmit(Request $request){

        $files = [];
       foreach($learnerFiles as $key => $learnerFile){   
           if(count($learnerFile) > 0){

                $path = $learnerFile->storeAs('public/uploads/learners', request('idNumber').'_'.$key.'.'.$learnerFile->extension());
                $search = 'public/' ;
                $trimmed = str_replace($search, '', $path) ;
                $file = FileUpload::create([

                    'user_id'     => $learner->id,
                    'file_name'   => $key,
                    'path'        => $trimmed
                ]);
            }
            else{

            }

        $file_object = new \stdClass();
        $file_object->name = $key;
        $file_object->size = round(Storage::size($path) / 1024, 2);
        $file_object->fileID = $learner->id;
        $files[] = $file_object;
        }

        return response()->json(array('files' => $photos), 200);        
    }

 if you have any query related this post then please do ask.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.