• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
How to show Image before upload JavaScript & HTML5 FileReader()
Home
HTML 5

How to show Image before upload JavaScript & HTML5 FileReader()

June 15th, 2025 Huzoor Bux API, HTML 5, JavaScript 1 comments

Facebook Twitter Google+ LinkedIn Pinterest

Show image before the upload is a requirement of every web application to give good user experience. We published a tutorial that doesn’t work in all browsers so; now I am going to give you this tutorial it will work on every browser which supports HTML5  File API and JavaScript check compatibility of API here. I hope you like this tutorial as all other tutorials.

DEMO
DOWNLOAD CODE

First of all, we need to know what FileReader is?

FileReader is an API of HTML5 used to interact with local files. Example of its capabilities, the FileReader API could be used to create a thumbnail preview of images as they’re being sent to the server. You could use client-side logic to verify an upload’s file type matches its file extension or restrict the size of an upload etc.

Browser compatibility for filereader api

Let’s start with HTML for single file show thumbnail.

<input type="file" id="file" name="file" />

<span id="output"></span>

That above code show you a file uploader you can select 1 file at a time.

JavaScript code to show image thumbnails

<script>

  function handleFileSelect(evt) {

    var file = evt.target.files; // FileList object



    var f = file[0];



      // Only process image files.

      if (!f.type.match('image.*')) {

        alert("Image only please....");

      }



      var reader = new FileReader();



      // Closure to capture the file information.

      reader.onload = (function(theFile) {

        return function(e) {

          // Render thumbnail.

          var span = document.createElement('span');

          span.innerHTML = ['<img class="thumb" title="', escape(theFile.name), '" src="', e.target.result, '" />'].join('');

          document.getElementById('output').insertBefore(span, null);

        };

      })(f);



      // Read in the image file as a data URL.

      reader.readAsDataURL(f);

    }

  



  document.getElementById('file').addEventListener('change', handleFileSelect, false);

</script>

That above JavaScript calls on change event of file element. This example can upload 1 file at a time for multiple files you can add a for a loop as below.

<script>

  function handleFileSelect(evt) {

    var file = evt.target.files; // FileList object



    // Loop through the FileList and render image files as thumbnails.

for (var i = 0, f; f = files[i]; i++) {



      // Only process image files.

      if (!f.type.match('image.*')) {

        alert("Image only please....");

      }



      var reader = new FileReader();



      // Closure to capture the file information.

      reader.onload = (function(theFile) {

        return function(e) {

          // Render thumbnail.

          var span = document.createElement('span');

          span.innerHTML = ['<img class="thumb" title="', escape(theFile.name), '" src="', e.target.result, '" />'].join('');

          document.getElementById('output').insertBefore(span, null);

        };

      })(f);



      // Read in the image file as a data URL.

      reader.readAsDataURL(f);

    }

}

  



  document.getElementById('file').addEventListener('change', handleFileSelect, false);

</script>

This will show you multiple files thumbnails after selection. for complete source code Download it and enjoy.

If you face any issue in its configuration please feel free to comment we love to help you.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • FileReader
  • How to
  • HTML5
  • javascript
Facebook Twitter Google+ LinkedIn Pinterest
Next article A Complete Guide to Full-Stack Development
Previous article Create HTML5 Fullscreen Static Video Background Using CSS

Huzoor Bux

I am a PHP Developer

Related Posts

Cool HTML5 features HTML 5
June 15th, 2025

Cool HTML5 features

Javascript Image Compress using HTML5 Canvas & File API before Upload HTML 5
June 15th, 2025

Javascript Image Compress using HTML5 Canvas & File API before Upload

Sanitizing input with PHP and JavaScript JavaScript
June 15th, 2025

Sanitizing input with PHP and JavaScript

1 Comments

  1. ibadullah
    November 2, 2020 at 10:35 am Reply ↓

    Hi bro

    do you have code script for newsletter?

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Cool HTML5 features
  • Using PHP and OOP Concepts to Build Custom WordPress Themes: A Modern Approach
  • 5 Mistakes that make you look like a noob in PHP
  • 12 Reasons to Choose PHP for Developing Website
  • 3 reasons why you SHOULD write tests
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.
 

Loading Comments...