Laravel PHP : Upload image with validation using jquery ajax form plugin

Laravel PHP : Upload image with validation using jquery ajax form plugin

In this post, I will let you know how to upload image file on server using jquery form plugin in Laravel PHP Framework.

In our last post, I have explained How to upload image with validation in Laravel.

Uploading image using jquery is in demand in most applications where you need to change your profile image.

This will be helpful because uploading image file using ajax won't reload the entire webpage.

Laravel provide different type of validation rules for image file, you can set the max file size, their mime type etc.

In this example, We will use ajax to upload image without page refresh and get preview of uploaded image.

Step 1 : Add routes

In this first step, Add following routes in your routes/web.php for ajax upload image.

  1. Route::get('get-image','ImageController@getImage');
  2. Route::post('ajax-upload-image', ['as'=>'ajax.upload-image','uses'=>'ImageController@ajaxUploadImage']);
Step 2 : Create ImageController

In this step, we will create ImageController in following path app/Http/Controllers/ImageController.

app/Http/Controllers/ImageController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class ImageController extends Controller
  5. {
  6.     public function getImage(){
  7.         return view('ajaxUploadImage');
  8.     }
  9.     public function ajaxUploadImage(Request $request){
  10.         $this->validate($request, [
  11.      'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  12.     ]);
  13. $image = time().'.'.$request->image->getClientOriginalExtension();
  14. $request->image->move(public_path('images'), $image);
  15. $url='images/'.$image;
  16. return response()->json(['url'=>$url]);
  17.     }
  18. }
Step 3 : Create View "ajaxUploadImage.blade.php" File

In this step, we will manage layouts and design form to upload files to send to the server.

To have jQuery form plugin we will include following library :

  • bootstrap.min.css
  • jquery.min.js
  • jquery.form.min.js
resources/views/ajaxUploadImage.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Laravel 5 - Ajax upload image to server without refresh the page with preview</title>
  5.     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6.     <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
  7.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.1/jquery.form.min.js"></script>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1>Laravel 5 - jQuery ajax upload image with example</h1>
  12. {!! Form::open(['route'=>'ajax.upload-image','files'=>'true']) !!}
  13.     <div class="alert alert-danger error-msg" style="display:none">
  14. <ul></ul>
  15. </div>
  16. <div class="preview-uploaded-image">
  17. </div>
  18.     <div class="form-group">
  19. <label>Image:</label>
  20. <input type="file" name="image" class="form-control">
  21. </div>
  22. <div class="form-group">
  23. <button class="btn btn-primary upload-image" type="submit">Upload Image</button>
  24. </div>
  25. {!! Form::close() !!}
  26. </div>
  27. <script type="text/javascript">
  28. $("body").on("click",".upload-image",function(e){
  29. $(this).parents("form").ajaxForm({
  30. complete: function(response)
  31. {
  32. if($.isEmptyObject(response.responseJSON.image)){
  33. $('.preview-uploaded-image').html('<img src="'+response.responseJSON.url+'">');
  34. }else{
  35. var msg=response.responseJSON.image;
  36. $(".error-msg").find("ul").html('');
  37. $(".error-msg").css('display','block');
  38. $.each( msg, function( key, value ) {
  39. $(".error-msg").find("ul").append('<li>'+value+'</li>');
  40. });
  41. }
  42. }
  43. });
  44. });
  45. </script>
  46. </body>
  47. </html>

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.