In this tutorial you will learn about the Laravel 8 jQuery Ajax File Upload Progress Bar Example and its application with practical example.
In this Laravel 8 jQuery Ajax file upload with progress bar tutorial I’ll show how to implement Ajax File Upload with progress bar in laravel 8. In this tutorial you will learn to implement progress bar along with ajax file upload. In this article I will share example to show you how to upload file using jquery ajax with progress bar in laravel 8. We will be using jquery ajax to upload file in laravel. We will also add progress bar with file uploading to display file upload progress.
jQuery Ajax File Upload Progress Bar Example
In this step by step tutorial I’ll demonstrate you how to upload file using jquery ajax with progress bar in laravel 8. Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Connecting App to Database
- Step 3 – Create Migration & Model
- Step 4 – Add Routes
- Step 5 – Create Controller by Artisan
- Step 6 – Create Blade View
- Step 7 – Run Development Server
- Step 8 – Test This App
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3 – Create Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Doc -m |
The above command will create a model Doc.php and a migration file for Docs table. Then go to database/migrations folder and open create_docs_table.php. Then put the following code into create_docs_table.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateDocsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('docs', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('docs'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4 – Create Route For File
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
use App\Http\Controllers\UploadFileController; Route::get('ajax-file-upload-progress-bar', [UploadFileController::class, 'index']); Route::post('store', [UploadFileController::class, 'store']); |
Step 5 – Generate Controller by Artisan
Now, lets create a controller named UploadFileController using command given below –
1 |
php artisan make:controller UploadFileController |
Once the above command executed, it will create a controller file UploadFileController.php in app/Http/Controllers/ directory. Open the UploadFileController.php file and put the following code in it.
app/Http/Controllers/UploadFileController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Doc; class UploadFileController extends Controller { public function index() { return view('progress-bar-file-upload'); } public function store(Request $request) { $request->validate([ 'file' => 'required', ]); $title = time().'.'.request()->file->getClientOriginalExtension(); $request->file->move(public_path('docs'), $title); $storeFile = new Doc; $storeFile->title = $title; $storeFile->save(); return response()->json(['success'=>'File Uploaded Successfully']); } } |
Step 6 – Create Blade View
In this step we will create blade view file. Now create a blade view file progress-bar-file-upload.blade.php. Go to /resources/views and create a file progress-bar-file-upload.blade.php. Then put the following code into progress-bar-file-upload.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Progress Bar File Upload Using Tutorial Example</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.js"></script> <style> .progress { position:relative; width:100%; } .bar { background-color: #b5076f; width:0%; height:20px; } .percent { position:absolute; display:inline-block; left:50%; color: #040608;} </style> </head> <body> <div class="container mt-5"> <div class="card"> <div class="card-header text-center"> <h2>Laravel 8 Progress Bar File Upload Using Ajax Tutorial</h2> </div> <div class="card-body"> <form method="POST" action="{{ url('ajax-file-upload-progress-bar') }}" enctype="multipart/form-data"> @csrf <div class="form-group"> <input name="file" type="file" class="form-control"><br/> <div class="progress"> <div class="bar"></div > <div class="percent">0%</div > </div> <br> <input type="submit" value="Submit" class="btn btn-primary"> </div> </form> </div> </div> </div> <script type="text/javascript"> var SITEURL = "{{URL('/')}}"; $(function() { $(document).ready(function() { var bar = $('.bar'); var percent = $('.percent'); $('form').ajaxForm({ beforeSend: function() { var percentVal = '0%'; bar.width(percentVal) percent.html(percentVal); }, uploadProgress: function(event, position, total, percentComplete) { var percentVal = percentComplete + '%'; bar.width(percentVal) percent.html(percentVal); }, complete: function(xhr) { alert('File Has Been Uploaded Successfully'); window.location.href = SITEURL +"/"+"ajax-file-upload-progress-bar"; } }); }); }); </script> </body> </html> |
Step 7 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 |
http://localhost:8000/ajax-file-upload-progress-bar OR http://localhost/blog/public/ajax-file-upload-progress-bar |