HTML5 display as image rather than “choose file” button


Use the JavaScript FileReader to allow users to choose an image.

Let us see an example −

<form id = "myform" runat = "server">
   <input type = 'file' id = "myimg" />
   <img id = "myid" src = "#" alt = "new image" />
</form>

Here is the JS −

function readURL(input) {
   if (input.files && input.files[0]) {
      var r = new FileReader();
      r.onload = function (ev) {
         $('#myid).attr('src', ev.target.result);
      }
      reader.readAsDataURL(input.files[0]);
   }
}   

Updated on: 25-Jun-2020

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements