Click here to Skip to main content
15,115,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
$(function(){
    Test = {
        UpdatePreview: function(obj){
          // if IE < 10 doesn't support FileReader
          if(!window.FileReader){
             // don't know how to proceed to assign src to image tag
          } else {
             var reader = new FileReader();
             var target = null;

             reader.onload = function(e) {
              target =  e.target || e.srcElement;
             $("#ImageId").attr("src", target.result);
			 };
              reader.readAsDataURL(obj.files[0]);
          }
        }
    };
});

my html code..............

What I have tried:

the above code is what i have try but the image is not show at all
Posted
Updated 10-Aug-20 1:24am
v2

1 solution

Use the JavaScript readAsDataURL() Method.

Refer: How to Preview an Image Before it is Uploaded Using jQuery[^]
Here, try this:
HTML
<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>jQuery Preview an Image Before it is Uploaded</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script>

        function previewFile(input){
            var file = $("input[type=file]").get(0).files[0];
            if(file){
                var reader = new FileReader();
                reader.onload = function(){
                    $("#previewImg").attr("src", reader.result);
                }
     
                reader.readAsDataURL(file);
            }
        }
    </script>
    </head> 
    <body>
        <form action="confirmation.php" method="post">
            <p>
                <input type="file" name="photo" onchange="previewFile(this);" required>
            </p>
            <img id="previewImg" src="/examples/images/transparent.png" alt="Placeholder">
            <p>
                <input type="submit" value="Submit">
            </p>
        </form>
    </body>
    </html>
   
Comments
Ibrahim Hassan 1234 9-Aug-20 15:32pm
   
im realy appreciate for your quick respond sir, doctor prof Sandeep Mewara.

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