<div class="avatar-wrap">
<img src="./imgs/avatar.png" alt="图片不可用" class='avatar-img'>
<input type="file" accept="image/*" class='avatar-ipt' hidden>
<button type="button" class='avatar-btn'>Choose File</button>
<div class='avatar-per'>0%</div>
</div>
let imgIpt = document.querySelector('.avatar-ipt')
let imgBtn = document.querySelector('.avatar-btn')
let img = document.querySelector('.avatar-img')
let imgPer = document.querySelector('.avatar-per')
imgBtn.addEventListener('click', () => {
imgIpt.click()
})
imgIpt.addEventListener('change', (e) => {
let file = e.target.files[0]
let fr = new FileReader()
fr.addEventListener("loadstart", (e) => {
console.log('fr start');
});
fr.addEventListener("load", (e) => {
console.log('fr loaded', e.target.result);
img.src = e.target.result;
});
fr.addEventListener("loadend", (e) => {
console.log('fr end');
});
fr.addEventListener("progress", (e) => {
console.log('fr progress', e);
if (e.lengthComputable) {
let per = Math.round(e.loaded / e.total * 100);
imgPer.innerHTML = per + '%';
}
});
fr.addEventListener("error", (e) => {
console.log('fr error');
});
fr.addEventListener("abort", (e) => {
console.log('fr abort');
});
fr.readAsDataURL(file);
})