반응형
undefined check
if(typeof obj === undefined)
if(obj === 'undefined')
input type file
check file selected
if(document.getElementById('file').value === '') {
alert('please select file')
}
read text file
function readFile() {
const fObj = document.getElementById("file");
console.log(fObj.value);
if(fObj.value === '') {
alert('파일을 선택해주세요.');
} else {
const selectedFile = fObj.files[0];
console.log(selectedFile);
console.log('Name :' + selectedFile.name + '/ Size : ' + selectedFile.size);
var reader = new FileReader();
reader.onload = function(evt) {
if(evt.target.readyState == FileReader.DONE) {
console.log(evt);
console.log(reader.result);
}
};
reader.readAsText(selectedFile);
}
}
read excel file - using sheetjs - need a check
function readExcelFile() {
const fObj = document.getElementById("file");
console.log(fObj.value);
if(fObj.value === '') {
alert('파일을 선택해주세요.');
} else {
const selectedFile = fObj.files[0];
console.log(selectedFile.path);
console.log('Name :' + selectedFile.name + '/ Size : ' + selectedFile.size);
var reader = new FileReader();
reader.onload = function(evt) {
if(evt.target.readyState == FileReader.DONE) {
console.log(reader.result);
//var workbook = XLSX.read(reader.result, {type: 'binary'});
var data = evt.target.result;
//data = new Uint8Array(data);
// call 'xlsx' to read the file
var workbook = XLSX.read(data, {type: 'binary'});
//var workbook = XLSX.readFile(selectedFile.path, {type: 'binary'});
console.log(workbook);
//var toHtml = XLSX.utils.sheet_to_html(workbook.Sheets['Sheet1'], {header:1});
//document.getElementById('grid').innerHTML = toHtml;
}
};
reader.readAsArrayBuffer(selectedFile);
reader.readAsBinaryString(selectedFile);
}
}
반응형
'엉터리 개발 이야기' 카테고리의 다른 글
[React] react-datetime 적용하기 (0) | 2018.03.06 |
---|---|
[React] React 시작하기, 디버깅 하기 (0) | 2018.03.06 |
npm cli command (0) | 2017.11.27 |
블로그 방문자수 조회 위젯 (0) | 2017.02.03 |
AWS 실습사이트 (0) | 2017.02.03 |