JavaScript本地图片文件预览

<body>
<input type="file" accept="image/*" onchange="loadFile(event)" />
<img id="previewContainer" src="" alt=""/>

<script>
    const output = document.querySelector("#previewContainer");
    const loadFile = function (event) {
        const reader = new FileReader();
        reader.onload = function () {
            console.log(reader);
            output.src = reader.result;
        };
        reader.readAsDataURL(event.target.files[0]);
    };
</script>
</body>
为之则易,不为则难。
原文地址:https://www.cnblogs.com/coderDemo/p/13353935.html