In this tutorial you will learn about the Laravel 7/6 Ajax Multiple Image Upload with Preview and its application with practical example.
In this Laravel Multiple Image Upload with Preview, I’ll show you how to upload image along with preview before upload in laravel. In this laravel multiple image upload example, I’ll show you how to validate upload image into folder and then save it into database. In this tutorial before saving image into database we will show preview of the images and then save it into directory. Before uploading the image we will display preview of the image. After successfully image upload into the database and folder we will display success message on the screen.
Laravel 7/6 Ajax Multiple Image Upload with Preview
- Install Laravel Fresh Setup
- Setup Database Credentials
- Create Route
- Generate Controller By Command
- Create the blade view
- Start Development Server
1: Install Laravel Fresh Setup
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 |
2: Setup Database Credentials
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 |
3: Create Route
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 |
Route::get('images-upload', 'ImageController@imagesUpload'); Route::post('images-upload', 'ImageController@imagesUploadPost')->name('images.upload'); |
4: Generate Controller by Command
Now, lets create a controller named ImageController using command given below –
1 |
php artisan make:controller ImageController |
The above command will create a controller name ImageController. Now, open controller app/HTTP/Controller/ImageController and put the following code in it:
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function imagesUpload() { return view('image'); } /** * Create a new controller instance. * * @return void */ public function imagesUploadPost(Request $request) { request()->validate([ 'uploadFile' => 'required', ]); foreach ($request->file('uploadFile') as $key => $value) { $imageName = time(). $key . '.' . $value->getClientOriginalExtension(); $value->move(public_path('images'), $imageName); } return response()->json(['success'=>'Images Uploaded Successfully.']); } } |
5: Create the blade view
Now, we will create some blade views files, Go to app/resources/views/ and create a blade file name image.blade.php and put the following code into your file:
image.blade.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 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<!DOCTYPE html> <html> <head> <title>Laravel Ajax Multiple Image Upload with Preview Example</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.min.js"></script> <style type="text/css"> input[type=file]{ display: inline; } #imgPreview{ border: 1px solid black; padding: 10px; } #imgPreview img{ width: 200px; padding: 5px; } </style> </head> <body> <div class="container"> <h1>Laravel Multiple Image Upload using Ajax with Preview Example</h1> <form action="{{ route('images.upload') }}" method="post" enctype="multipart/form-data"> {{ csrf_field() }} <input type="file" id="uploadFile" name="uploadFile[]" multiple/> <input type="submit" class="btn btn-success" name='submitImage' value="Upload Image"/> </form> <br/> <div id="imgPreview"></div> </div> </body> <script type="text/javascript"> $("#uploadFile").change(function(){ $('#imgPreview').html(""); var total_file=document.getElementById("uploadFile").files.length; for(var i=0;i<total_file;i++) { $('#imgPreview').append("<img src='"+URL.createObjectURL(event.target.files[i])+"'>"); } }); $('form').ajaxForm(function() { alert("Uploaded SuccessFully"); }); </script> </html> |
6: 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 |
http://localhost:8000/images-upload |