Skip to main content

Preview Image before Upload


In this blog, i will be showing how to display selected images to upload before upload in your browser.


First of all, we should have input fields of type file to select images from our drives.


<label>Single File:</label>
<input type="file" id="fileSingle" name="fileSingle" />
<label>Multiple Files:</label>
<input type="file" id="fileMultiple" name="fileMultiple[]" multiple />


<span id="outputSingle"></span> // span to display single selected image
<span id="outputMultiple"></span> // span to display multiple selected images.



Now we should add two event listener to get event 'change' for these two fields.


<script>
    $(document).ready(function() {
        var multifilediv = document.getElementById('fileMultiple');
        if (multifilediv) {
            multifilediv.addEventListener('change', MultipleFilehandler, false);
        }
        var filediv = document.getElementById('fileSingle');
        if (filediv) {
            filediv.addEventListener('change', SingleFileHandler, false);
        }
    });
</script>




Now we should write two functions SingleFileHandler and MultipleFileHandler to display selected image.


<script>
    function SingleFileHandler(evt) {
        var file = evt.target.files;
        var f = file[0];
        if (!f.type.match('image.*')) {
            alert("Please Select Image.");
        }
        var file_reader = new FileReader();
        file_reader.onload = (function(theFile) {
            return function(e) {
                var span = document.createElement('span');
                span.innerHTML = ['<img class="img-thumbnail" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
                document.getElementById('outputSingle').innerHTML = "";
                document.getElementById('outputSingle').insertBefore(span, null);
            };
        })(f);


        file_reader.readAsDataURL(f);
    }



    function MultipleFilehandler(evt) {
        var files = evt.target.files; // FileList object
        document.getElementById('outputMultiple').innerHTML = "";
        for (var i = 0, f; f = files[i]; i++) {




            if (!f.type.match('image.*')) {
                alert("Please select Images only.");
            }
            var file_reader = new FileReader();
            file_reader.onload = (function(theFile) {
                return function(e) {
                    var span = document.createElement('span');
                    span.innerHTML = ['<img class="img-thumbnail" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
                    document.getElementById('outputMultiple').insertBefore(span, null);
                };
            })(f);
            file_reader.readAsDataURL(f);
        }
    }
</script>


By now selected images will be displayed in your browser. You can download complete code from https://github.com/ijasnahamed/preview-image-before-upload

Comments

Popular posts from this blog

Implementing Client Side WebRTC using Sipml5 javascript

Step 1:

Download and require Sipml5 library function.

Step 2:

Initialize sipml5 Engine in your web page :

var readyCallback = function(e) {
// function called when sipml is successfully initialised.
createSipStack(); // calling this function to create sip stack(see below)
};

var errorCallback = function(e) {
// function called when error occured during sipml initialisation.
};

SIPml.init(readyCallback, errorCallback);

Step 3:

Create Sip Stack :-
Sip Stack is an object that must be created before making/receiving call and sms. Creating Sip stack is an asynchronous process, so you need to create an event listener function to get state change notification.

var sipStack;

function EventListener(e) {

/*
* e.type ;type of event listener
* e.session ; current event session
* e.getSipResponseCode() ; event response code
* e.description ; event description
*/

if(e.type == 'started'){
// successfully started the stack.
register();
} else if(e.type == 'i_new_call'){
// when new incoming call comes.
      …

Setup Asterisk 13 with FreePBX 13 in CentOS 7

Launch CentOS 7 AWS Ec2 InstanceLog in to your aws consoleGo to ec2 management page and click Launch Instance on Instance pageIn Choose AMI step, go to AWS MarketPlace tab and search CentOS 7 on search field. List of centos 7 ami's will be available.Select CentOS 7 (x86_64) ami which is free tier eligible. Select your instance type and click configure instance. Keep default values from Configure Instance to Add Tags stepsIn configure security group, create a security group which allow minimum ports openSSH : port 22HTTP : port 80Custom UDP Rule : 10000-20000 (if you are giving RTP ports 10000-20000)Custom UDP Rule : 5060 (ChanSIP port)Custom TCP Rule : 5060 (ChanSIP Port)Click Launch Instance. Select your key file and accept terms to launch instance.
Setup CentOS Server SSH into newly created CentOS server with username centos and your key file.Update all existing packagessudo yum update -yNow start http service(you can access your server via browser by going to your server ip addres…

Twilio - Record a complete incoming call

Currently, Twilio services does not provide to record the whole incoming call. Only dial initiated within the incoming call can be recorded. But in day to day life, there are many occasion we need to record the whole incoming call such as an ivr call or a customer service call. So, by taking the twilio feature that child call can be recorded, i have developed a call flow using two twilio numbers so that we can record the complete call from caller.
For demonstrating, i have created a small ivr. When caller calls twilio number. It will be asked a gather "whether he/she is interested in taking to our agent? if yes press one or not press two or just hangup". If caller press one, call will be redirected to an agent.

If we try above call method, we get only the recording of agent call. We misses what happened after twilio answer and before agent call. So to get whole call recording, i have developed another mechanism as below

When caller calls twilio number A, twilio will ans…