HTML本地资源读取!

时间:2022-02-10 09:12:15
const file = document.getElementById('file') as HTMLInputElement;
const imga = document.getElementsByTagName('img')[0] as HTMLImageElement;


file.addEventListener('change', function () {
    let reader = new FileReader() as FileReader;

    let file = this.files as FileList;
    // reader.readAsText(file[0]) //文件
    reader.readAsDataURL(file[0]); //二进制

    // console.log(reader);
    reader.onload = function () {
        console.log(this.result);
        imga.src = `${this.result}`
    }

})