javascript - jquery upload and view image -
i found jquery script lets preview image before upload it.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script> function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#target').attr('src', e.target.result); } reader.readasdataurl(input.files[0]); } } $("#imginp").change(function(){ readurl(this); }); </script> </head> <body> <h1>jquery uploads</h1> <form id="form1" runat="server"> <input type="file" id="imginp" /> <img id="target" src="#" alt="your image" /> </form> </body> </html> i found on jsfiddle , works fine. try run on server , doesn't work. can't seem figure out why not working. have ideas?
i couple different things happening here. first guess there timing issue of when javascript loading. try throwing jquery listener "ready" call.
function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#target').attr('src', e.target.result); } reader.readasdataurl(input.files[0]); } } $(function(){ $("#imginp").change(function(){ readurl(this); }); }) also - make sure protocol (http vs https) matching pull code.jquery.com
Comments
Post a Comment