In this tutorial you will learn about the Laravel 7 Ajax File Upload with Progress Bar and its application with practical example.
In this Laravel 7 file upload with progress bar tutorial I’ll show how to implement Ajax File Upload with progress bar in laravel. In this tutorial you will learn to implement progress bar along with ajax file upload. In this step by step tutorial I’ll share example to demonstrate the ajax image uploading with progress bar.
Laravel 7 Ajax File Upload with Progress Bar
- Step 1: Install Laravel App For Progress Bar
- Step 2: Add Database Details
- 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: Live Demo
Step 1: Install Laravel App For Progress Bar
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: Add Database Details
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 |
This command will create one model name Doc.php and as well as one migration file for the Docs table.Now, go to database/migrations folder and open create_docs_table.php. Then update 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, in this step we will create model and migration file. Please run the following 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 |
Route::get('ajax-file-upload-progress-bar', 'UploadFileController@index'); Route::post('store', 'UploadFileController@store'); |
Step 5: Generate Controller by Artisan
Now, lets create a controller named UploadFileController using command given below –
1 |
php artisan make:controller UploadFileController |
The above command will create a controller named UploadFileController.php file.Now go to app/http/controllers/ folder and open UploadFileController.php. Then add the following file uploading methods into your UploadFileController.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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\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, create one blade view file named progress-bar-file-upload.blade.php. Go to /resources/views and create one file name progress-bar-file-upload.blade.php. Then put the following code into your 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 64 65 66 67 68 69 70 71 72 73 74 |
<!DOCTYPE html> <html> <head> <title>Laravel 7 Progress Bar File Upload Using Tutorial</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 7 Progress Bar File Upload Using 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 2 3 4 5 |
php artisan serve If you want to run the project diffrent port so use this below command php artisan serve --port=8080 |
Now, open the following URL in browser to see the output –
1 2 3 4 5 |
http://localhost:8000/ajax-file-upload-progress-bar OR hit in browser http://localhost/blog/public/ajax-file-upload-progress-bar |