前端js讀取文件內容

<input v-if="isShowFile" type="file" id="file" @change="onFileChange"/>
onFileChange(files) {
    if (files.length) {
        var file = files[0];
        var reader = new FileReader();    // new一個FileReader實例
        if (/text+/.test(file.type)) {    // 判斷文件類型,是不是text類型
            reader.onload = function() {
                console.log('文件內容', this.result);
            } 
        }else if(/image+/.test(file.type)) {    // 判斷文件是不是imgage類型
            //判斷文件是不是imgage類型
            $('body').append('<img src="' + this.result + '"/>');
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章