abelalvarez89 / xlsx-parse-json

MIT License
13 stars 4 forks source link

FileReader is not defined #3

Closed LukaszWiktor closed 6 years ago

LukaszWiktor commented 6 years ago

I was trying to use xlsx-parse-json in a Node.js script but I got following error:

(node:8236) UnhandledPromiseRejectionWarning: ReferenceError: FileReader is not defined
at [...]\node_modules\xlsx-parse-json\index.js:9:16
abelalvarez89 commented 6 years ago

The library expects a file to be passed in. Can you also provide a snippet of your code, so I can see in context how you are using it.

LukaszWiktor commented 6 years ago

Here is my code snippet:

const xlsxParser = require("xlsx-parse-json");
const file = "Test.xlsx";

xlsxParser
  .onFileSelection(file)
  .then(data => {
    console.log(data);
  });

I think that the reason of this error is that FileReader is available only in browsers and not in node.js.

abelalvarez89 commented 6 years ago

The library expects the actual file. Passing the file name is not going to work. Below is an example of what I mean using AngularJs. Im using in this example ngFileUpload to get the file from the computer into the application, then passing it to my library. In your case you have to upload the file then use xlsx-parse-json to parse it.

The purpose of this library is not to upload a file, but to just parse the uploaded file.

'use strict';

var angular = require('angular');

require('ng-file-upload');

angular.module('test-xls-parser', ['ngFileUpload'])
.component('testXlsParserUpload', {
    template: '<input type="file" ngf-select="vm.uploadFile($file)" ng-disabled="vm.disableUpload" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/><pre>{{vm.data | json}}</pre>',
    controller: function() {
        var vm = this;
        var xlsParser = require('xlsx-parse-json');

        vm.uploadFile = function(file){
            xlsParser.onFileSelection(file)
            .then((data) => {
                vm.data = data;
            });
        }
    },
    controllerAs: 'vm'
})
CherryDT commented 3 years ago

OK but how can we use that in node.js then? That was the question and it isn't clear. What do you mean when you say "a file"? We have a Buffer (or a binary string, if you want) with the contents of the XLSX file - what are we supposed to pass in? You wrote the purpose is to parse the uploaded file, but it's not clear how the data of the uploaded file (on the server-side) is supposed to be passed in...

abelalvarez89 commented 3 years ago

@CherryDT hello, currently only supporting web applications. The logic can be expanded, but its a limitation in the current state