Image Upload Preview using JavaScript

In this tutorial, we are going to learn about how to Preview Image in File Upload using JavaScript. In this simple tutorial, we are sending the image using form field to the script where we can preview the current image in the form field.

Creating Markup

HTML source code for the form field contains the file input and the default image as shown in the image below. Result This is the HTML source code of the image above.
  1. <div class="modal-body">
  2. <form method="post" action="edit_profile_cover_image.php" enctype="multipart/form-data">
  3. <input id="coverFileUpload" multiple="multiple" name="image" type="file" class="file_style" required/>
  4. <div id="image-holder-cover" style="width: 530px; border-radius: 10px; border: 1px solid #286090; text-align:center; height: 160px; margin-left: 345px; margin-top: -2px;">
  5. <img src="images/cover-preview.jpg" style="width: 530px; border-radius: 10px; border: 1px solid #286090; text-align:center; height: 160px; margin-top: -2px;">
  6. </div>
  7. </div>
  8. <div class="modal-footer">
  9. <button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
  10. <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-save"></i> Save</button>
  11. </form>
  12. </div>

Construct Script Source Code

This simple script that we are going to use to preview the image before upload it.
  1. <script>
  2. $(document).ready(function() {
  3. $("#coverFileUpload").on('change', function() {
  4. //Get count of selected files
  5. var countFiles = $(this)[0].files.length;
  6. var imgPath = $(this)[0].value;
  7. var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
  8. var image_holder = $("#image-holder-cover");
  9. image_holder.empty();
  10. if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
  11. if (typeof(FileReader) != "undefined") {
  12. //loop for each file selected for uploaded.
  13. for (var i = 0; i < countFiles; i++)
  14. {
  15. var reader = new FileReader();
  16. reader.onload = function(e) {
  17. $("<img />", {
  18. "src": e.target.result,
  19. "class": "thumb-image"
  20. }).appendTo(image_holder);
  21. }
  22. image_holder.show();
  23. reader.readAsDataURL($(this)[0].files[i]);
  24. }
  25. } else {
  26. alert("This browser does not support FileReader.");
  27. }
  28. } else {
  29. alert("Pls select only images");
  30. }
  31. });
  32. });
  33. </script>

The CSS Source Code

Using this CSS source code to control the width and height of the image or you can add any design to preview the image elegant.
  1. .thumb-image {
  2. width: 160px;
  3. border-radius: 10px;
  4. border: 1px solid #286090;
  5. height: 160px;
  6. /* margin-left: 345px;
  7. margin-top: -25px; */
  8. }
  9.  
  10. .file_style {
  11. float: left;
  12. margin-top: 65px;
  13. border: 1px solid blue;
  14. padding: 10px;
  15. color: blue;
  16. font-size: 15px;
  17. font-family: cursive;
  18. font-weight: bold;
  19. }
  20.  
  21. .preview_style_text {
  22. color:blue;
  23. font-size:18px;
  24. font-family:cursive;
  25. font-weight:bold;
  26. }
  27.  
  28. /*sliding text in image*/
  29.  
  30. #div1 {
  31. display: block;
  32. position: relative;
  33. float: left;
  34. overflow: hidden;
  35. margin-bottom:15px;
  36. }
  37. figcaption {
  38. position: absolute;
  39. background: black;
  40. background: rgba(0,0,0,0.75);
  41. color: white;
  42. padding: 10px 20px;
  43. opacity: 0;
  44. -webkit-transition: all 0.6s ease;
  45. -moz-transition: all 0.6s ease;
  46. -o-transition: all 0.6s ease;
  47. }
  48. #div1:hover figcaption {
  49. opacity: 1;
  50. }
  51. #div1:before {
  52. position: absolute;
  53. font-weight: 800;
  54. background: black;
  55. background: rgba(255,255,255,0.75);
  56. text-shadow: 0 0 5px white;
  57. color: black;
  58. width: 24px;
  59. height: 24px;
  60. -webkit-border-radius: 12px;
  61. -moz-border-radius: 12px;
  62. border-radius: 12px;
  63. text-align: center;
  64. font-size: 14px;
  65. line-height: 24px;
  66. -moz-transition: all 0.6s ease;
  67. opacity: 0.75;
  68. }
  69. #div1:hover:before {
  70. opacity: 0;
  71. }
  72.  
  73. .cap-left:before { bottom: 10px; left: 10px; }
  74. .cap-left figcaption { bottom: 0; left: -30%; }
  75. .cap-left:hover figcaption { left: 0; }
  76.  
  77. .cap-right:before { bottom: 10px; right: 10px; }
  78. .cap-right figcaption { bottom: 0; right: -30%; }
  79. .cap-right:hover figcaption { right: 0; }
  80.  
  81. .cap-top:before { top: 10px; left: 10px; }
  82. .cap-top figcaption { left: 0; top: -30%; }
  83. .cap-top:hover figcaption { top: 0; }
  84.  
  85. .cap-bot:before { bottom: 10px; left: 10px; }
  86. .cap-bot figcaption { left: 0; bottom: -30%;}
  87. .cap-bot:hover figcaption { bottom: 0; }

Output


Originally the form and the default image will be, ResultAfter uploading the image using the script that we construct, we are going to load or preview the target image to the right side of our form field. So, this would be the output. Result Kindly click the "Download Code" button below for full source code. Thank you very much. Hope that this tutorial will help you a lot. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment