I had a requirement of reading excel file which I have done by using some library. Here I'm getting user selected file and passing it to the handleFile function, this function is converting excel data to JSON data. It is working fine.
var xlf = document.getElementById('xlf');
function handleFile(xlf) {
var files = xlf.files;
var f = files[0];
{
//some code
}
}
But my requirement got changed, now user will upload the zip file which contains the list of excel files. From the zip file I need to read one by one excel file and it's data.
To unzip the uploaded file I'm using the zip.js library [ https://gildas-lormeau.github.io/zip.js/demos/demo2.html ]. They have a given some demo, in that if we upload the zip file they are displaying the file list on screen and by clicking on the displayed file, it will be downloaded. In their code they are creating Entry Object and adding files information to that object, something like below
reader.readUint8Array(datalength, reader.size - datalength, function(bytes) {
var i, index = 0, entries = [], entry, filename, comment, data = getDataHelper(bytes.length, bytes);
for (i = 0; i < fileslength; i++) {
entry = new Entry();
entry._worker = worker;
if (data.view.getUint32(index) != 0x504b0102) {
onerror(ERR_BAD_FORMAT);
return;
}
readCommonHeader(entry, data, index + 6, true, onerror);
entry.commentLength = data.view.getUint16(index + 32, true);
entry.directory = ((data.view.getUint8(index + 38) & 0x10) == 0x10);
entry.offset = data.view.getUint32(index + 42, true);
filename = getString(data.array.subarray(index + 46, index + 46 + entry.filenameLength));
entry.filename = ((entry.bitFlag & 0x0800) === 0x0800) ? decodeUTF8(filename) : decodeASCII(filename);
if (!entry.directory && entry.filename.charAt(entry.filename.length - 1) == "/")
entry.directory = true;
comment = getString(data.array.subarray(index + 46 + entry.filenameLength + entry.extraFieldLength, index + 46
+ entry.filenameLength + entry.extraFieldLength + entry.commentLength));
entry.comment = ((entry.bitFlag & 0x0800) === 0x0800) ? decodeUTF8(comment) : decodeASCII(comment);
entries.push(entry);
index += 46 + entry.filenameLength + entry.extraFieldLength + entry.commentLength;
}
and while displaying the file list on screen, they are doing like below
model.getEntries(fileInput.files[0], function(entries) {
fileList.innerHTML = "";
entries.forEach(function(entry) {
var li = document.createElement("li");
var a = document.createElement("a");
a.textContent = entry.filename;
a.href = "#";
a.addEventListener("click", function(event) {
if (!a.download) {
download(entry, li, a);
event.preventDefault();
return false;
}
}, false);
li.appendChild(a);
fileList.appendChild(li);
});
});
Here each entry object represent a file inside the zip file. Here I want to call handleFile method to read the excel file. but handleFile() method is expecting the file input element but from the zip file I got the entry object.
when I pass the entry object to the handleFile method, it is not working, could you please help me to resolve this issue.
function handleFile(xlf) {
var files = xlf.files;
var f = files[0];
{
//some code
}