<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>图片本地预览示例</title>
</head>
<body>
<h3>图片本地预览示例</h3>
<input type="file" accept="image/*" οnchange="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>
</html>