如何在 HTML 和 JavaScript 中预览上传前后的图像?
若要在上传前和上传后预览图像,你需要尝试以下代码 − HTML
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <form id = "form1" runat = "server"> <input type ='file' id = "demo" /> <img id = "myid" src = "#" alt = "new image" /> </form>
以下是 jQuery −
function display(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(event) { $('#myid').attr('src', event.target.result); } reader.readAsDataURL(input.files[0]); } } $("#demo").change(function() { display(this); });
广告