Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Preview Image Before Uploading Using JQuery

HTML accompanies different kinds of inputs like text, password, email, and so forth and all of these information sources are for forms. There is input type “file” which is for Uploading a file, we simply need to put a tag to making this. Of course, it accompanies essential styles yet we can modify its style and function by utilizing JS and CSS. In this article, we’ll show you how to create a custom file input to preview image before uploading using jQuery and CSS.

Image Preview Feature

The preview image feature lets the clients see the chosen picture before uploading. With this component, the picture is shown on the website page quickly once a file (image) is chosen by an input field. Verifying the picture before uploading is an extremely valuable component for the file transfer usefulness.

This feature helps the client to ensure before uploading the file and change the picked image if necessary. From the client’s point of view, it is useful for uploading an ideal picture or file without doing the monotonous uploading.

Steps To Create Custom Input To Preview Image Before Uploading

In this program, we will tell you the best way to show a preview image before uploading using jQuery.

For making this program you need to make 3 files. The first is for HTML, the second is for CSS, and the third for JavaScript. Follow the steps as shown below:

1. Create HTML Form For Uploading Image File

First, we create an Index File to make an HTML form. We also need to link our JS and CSS files in the index file for the styling and functioning of our program. Now copy the code below and paste it in your index file:



  Image Upload Preview | howto-solution.com

2. Create CSS File For Styling Elements

The CSS file is created for styling our elements and to provide the values like padding, margins, position, etc. Now simply create a CSS file and copy-paste the code below:

}
.contain{
  margin-left:auto;
  margin-right:auto;  
  margin-top:calc(calc(100vh - 405px)/2);
}
#form1{
  width:auto;
}
.alert{
  text-align:center;
}

#blah{  
  max-height:256px;
  height:auto;
  width:auto;
  display:block;
  margin-left: auto;
   margin-right: auto;
  padding:5px;
}
#img_contain{
  border-radius:5px;
  /*  border:1px solid grey;*/
  margin-top:20px;
  width:auto;  
}
.input-group{  
  margin-left:calc(calc(100vw - 320px)/2);
  margin-top:40px;
  width:320px;
}
.imgInp{  
  width:150px;
  margin-top:10px;
  padding:10px;
  background-color:#d3d3d3;  
}
.loading{
   animation:blinkingText ease 2.5s infinite;
}
@keyframes blinkingText{
    0%{     color: #000;    }     
    50%{   color: #transparent; }
    99%{    color:transparent;  }
    100%{ color:#000; }
}
.custom-file-label{
  cursor:pointer;
}

3. Create The Javascript File To Handle The Image

It is one of the most important step, as from here we’ll be handling the program to function accordingly. Create a JS file and copy-paste the code given below:

$("#inputGroupFile01").change(function(event)
{  
  RecurFadeIn();
  readURL(this);    
});

$("#inputGroupFile01").on('click',function(event){
  RecurFadeIn();
});

function readURL(input)
{    
 if (input.files && input.files[0])
      {   
        var reader = new FileReader();
        var filename = $("#inputGroupFile01").val();
        filename = filename.substring(filename.lastIndexOf('\\')+1);
        reader.onload = function(e)
         {
           debugger;      
           $('#blah').attr('src', e.target.result);
           $('#blah').hide();
           $('#blah').fadeIn(500);      
           $('.custom-file-label').text(filename);             
         }
        reader.readAsDataURL(input.files[0]);    
      }
  $(".alert").removeClass("loading").hide();
}

function RecurFadeIn()
{ 
  console.log('ran');
  FadeInAlert("Wait for it...");  
}

function FadeInAlert(text)
{
  $(".alert").show();
  $(".alert").text(text).addClass("loading");  
}

Explanation Of Code:

The readURL() function above is used to read the raw file data and generate a preview of it. The FileReader is the object for reading the file using JS. When the file content is stacked, the image preview is attached to the HTML component using jQuery.

After this, the jQuery change() method triggers the readURL() function when a file is chosen. The readURL() function generated a preview of the chosen file and displays it on the web page.

Upload File To The Server Using PHP

You can send the image file to the database server to store your image file once it’s uploaded. You can transfer files to the server using PHP.

First, you need to fetch the file information using $_FILES in PHP, then use move_uploaded_file() function to upload your file. Just follow the code below:

Now, the only thing left is to finally test it. We hope you liked our guide to create Image Upload With Preview Using jQuery and CSSCustom File Input Image program.

The post How To Preview Image Before Uploading Using JQuery appeared first on How to Solution.



This post first appeared on Career Options After Btech Or Other Graduation, please read the originial post: here

Share the post

How To Preview Image Before Uploading Using JQuery

×

Subscribe to Career Options After Btech Or Other Graduation

Get updates delivered right to your inbox!

Thank you for your subscription

×