Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I chose the photo without having to upload a picture to be displayed web page



XML
<input type="file" class="file" id="file" name="file" title="Please upload"/>

<canvas id="myCanvas" width="250" height="300" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
Posted

1 solution

You can use FileReader web-api object for this, see this example:
In Html code:
HTML
<input id="src" type="file" onchange="fileChange()" />
<img id="target" />

In javascript:
JavaScript
function fileChange(){
   var src = document.getElementById("src");
   var target = document.getElementById("target");
   showImage(src,target);
}

function showImage(src,target) {
  var fr=new FileReader();
  // when image is loaded, set the src of the image where you want to display it
  fr.onload = function(e) { target.src = this.result; };
  src.addEventListener("change",function() {
    // fill fr with image data    
    fr.readAsDataURL(src.files[0]);
  });
}
 
Share this answer
 
Comments
Member 9160577 19-Jun-14 8:00am    
Thank you for your help
Abhinaw Kumar Singh 19-Jun-14 8:04am    
:)
Member 9160577 21-Jun-14 2:46am    
hello
i used the code you suggested but it does not display the first photo i choose and i have to do it a second time
Abhinaw Kumar Singh 2-Jul-14 8:37am    
you should remove the change listener..

function showImage(src,target) {
var fr=new FileReader();
fr.onload = function(e) { target.src = this.result; };
fr.readAsDataURL(src.files[0]);

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900