In this tutorial you will learn about the Laravel 9 jQuery Ajax File Upload Progress Bar Example and its application with practical example.
In this Laravel 9 jQuery Ajax file upload with progress bar tutorial I’ll show how to implement Ajax File Upload with progress bar in laravel 9. 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 9. We will be using jquery ajax to upload file in laravel 9. We will also add progress bar with file uploading to display file upload progress.
Laravel 9 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 9. Please follow the instruction given below:
- Install Laravel 9
- Connecting App to Database
- Create Migration & Model
- Add Routes
- Create Controller by Artisan
- Create Blade View
- Run Development Server
- Test This App
Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
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 |
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 |
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']); |
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']); } } |
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 9 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 9 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> |
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 |