Laravel PHP : Upload image with validation using jquery ajax form plugin
In this post we will give you information about Laravel PHP : Upload image with validation using jquery ajax form plugin. Hear we will give you detail about Laravel PHP : Upload image with validation using jquery ajax form pluginAnd how to use it also give you demo for it if it is necessary.
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.
- Route::get('get-image','ImageController@getImage');
- Route::post('ajax-upload-image',['as'=>'ajax.upload-image','uses'=>'ImageController@ajaxUploadImage']);
Route::get('get-image','ImageController@getImage'); 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
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- class ImageController extends Controller
- {
- public functiongetImage(){
- returnview('ajaxUploadImage');
- }
- public functionajaxUploadImage(Request $request){
- $this->validate($request,[
- 'image'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
- ]);
- $image=time().'.'.$request->image->getClientOriginalExtension();
- $request->image->move(public_path('images'),$image);
- $url='images/'.$image;
- returnresponse()->json(['url'=>$url]);
- }
- }
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class ImageController extends Controller { public function getImage(){ return view('ajaxUploadImage'); } public function ajaxUploadImage(Request $request){ $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); $image = time().'.'.$request->image->getClientOriginalExtension(); $request->image->move(public_path('images'), $image); $url='images/'.$image; return response()->json(['url'=>$url]); } }
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
- <!DOCTYPEhtml>
- <html>
- <head>
- <title>Laravel 5 - Ajax upload image to server without refresh the page with preview</title>
- <linkrel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <scriptsrc="http://code.jquery.com/jquery-3.2.1.min.js"></script>
- <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.1/jquery.form.min.js"></script>
- </head>
- <body>
- <divclass="container">
- <h1>Laravel 5 - jQuery ajax upload image with example</h1>
- {!! Form::open(['route'=>'ajax.upload-image','files'=>'true']) !!}
- <divclass="alert alert-danger error-msg"style="display:none">
- <ul></ul>
- </div>
- <divclass="preview-uploaded-image">
- </div>
- <divclass="form-group">
- <label>Image:</label>
- <inputtype="file"name="image"class="form-control">
- </div>
- <divclass="form-group">
- <buttonclass="btn btn-primary upload-image"type="submit">Upload Image</button>
- </div>
- {!! Form::close() !!}
- </div>
- <scripttype="text/javascript">
- $("body").on("click",".upload-image",function(e){
- $(this).parents("form").ajaxForm({
- complete: function(response)
- {
- if($.isEmptyObject(response.responseJSON.image)){
- $('.preview-uploaded-image').html('<imgsrc="'+response.responseJSON.url+'">');
- }else{
- var msg=response.responseJSON.image;
- $(".error-msg").find("ul").html('');
- $(".error-msg").css('display','block');
- $.each( msg, function( key, value ) {
- $(".error-msg").find("ul").append('<li>'+value+'</li>');
- });
- }
- }
- });
- });
- </script>
- </body>
- </html>
<!DOCTYPE html> <html> <head> <title>Laravel 5 - Ajax upload image to server without refresh the page with preview</title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.1/jquery.form.min.js"></script> </head> <body> <div > <h1>Laravel 5 - jQuery ajax upload image with example</h1> {!! Form::open(['route'=>'ajax.upload-image','files'=>'true']) !!} <div style="display:none"> <ul></ul> </div> <div > </div> <div > <label>Image:</label> <input type="file" name="image" > </div> <div > <button type="submit">Upload Image</button> </div> {!! Form::close() !!} </div> <script type="text/javascript"> $("body").on("click",".upload-image",function(e){ $(this).parents("form").ajaxForm({ complete: function(response) { if($.isEmptyObject(response.responseJSON.image)){ $('.preview-uploaded-image').html('<img src="'+response.responseJSON.url+'">'); }else{ var msg=response.responseJSON.image; $(".error-msg").find("ul").html(''); $(".error-msg").css('display','block'); $.each( msg, function( key, value ) { $(".error-msg").find("ul").append('<li>'+value+'</li>'); }); } } }); }); </script> </body> </html>
Hope this code and post will helped you for implement Laravel PHP : Upload image with validation using jquery ajax form plugin. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs