JavaScript实现图片预览

    xiaoxiao2021-03-25  94

    今天在开发一个上传图片的功能时,图片上传到服务器以后,在显示在客户端,需要等待好长时间,于是查了查JS实现突破预览功能,在此记录一下,以备下次使用时,可以迅速找到;代码如下:

    <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>js图片预览功能</title> <script language=javascript> function previewFile() { var preview = document.querySelector('img'); var file = document.querySelector('input[type=file]').files[0]; var reader = new FileReader(); reader.onloadend = function () { preview.src = reader.result; } if (file) { reader.readAsDataURL(file); } else { preview.src = ""; } } </script> </head> <body> <input type="file" onchange="previewFile()"><br> <img src="" height="200" width="300" alt="Image preview..."/> </body> </html>
    转载请注明原文地址: https://ju.6miu.com/read-22174.html

    最新回复(0)