Sheetjs: readFile returns Error ERR_INVALID_ARG_VALUE

Created on 18 May 2018  路  1Comment  路  Source: SheetJS/sheetjs

Using the multer package to parse the xlsx in express js:

Code:

const XLSX = require('xlsx');
const multer = require('multer');
const upload = multer();

module.exports = function(app){

    app.post('/upload', upload.any(), function(req, res) {
        let uploadedFile = req.files[0].buffer;
        console.log("uploadedFile: ", uploadedFile);
        var workbook = XLSX.readFile(uploadedFile, {inputType: "buffer"});

        res.sendStatus(200);
    });

}

Output:

uploadedFile: 
Buffer(96769) [80, 75, 3, 4, 20, 0, 6, 0, 鈥
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 c0 ea 9a 4d e0 01 00 00 7a 0e 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 ...
    at Object.fs.openSync (fs.js:552:3)
    at Object.fs.readFileSync (fs.js:465:33)
    at read_binary (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/xlsx/xlsx.js:1891:44)
    at readSync (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/xlsx/xlsx.js:19481:69)
    at Object.readFileSync (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/xlsx/xlsx.js:19509:9)
    at /Users/jonas/Workspace/auto_calendar_js_backend/src/routes.js:10:29
    at Layer.handle [as handle_request] (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/express/lib/router/route.js:137:13)
    at Array.<anonymous> (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/multer/lib/make-middleware.js:53:37)
    at listener (/Users/jonas/Workspace/auto_calendar_js_backend/node_modules/on-finished/index.js:169:15)

Using Postman to perform the POST request with the xlsx sheet inside the form data

It tries to read the file via a path, but i don't want to save the file first on the server to then serve it back to xlsx via path. I prefer to directly pass it so that it's stateless.

I tried to add the {inputType: "buffer"}, but it still tries to read it as a path..

Any idea's?

Most helpful comment

readFile always interprets the argument as a file name. Use read with the option type:"buffer":

        let uploadedFile = req.files[0].buffer;
        console.log("uploadedFile: ", uploadedFile);
        var workbook = XLSX.read(uploadedFile, {type: "buffer"});

>All comments

readFile always interprets the argument as a file name. Use read with the option type:"buffer":

        let uploadedFile = req.files[0].buffer;
        console.log("uploadedFile: ", uploadedFile);
        var workbook = XLSX.read(uploadedFile, {type: "buffer"});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

upasana-shah picture upasana-shah  路  4Comments

Sankrish picture Sankrish  路  4Comments

mmancosu picture mmancosu  路  3Comments

seanmcilvenna picture seanmcilvenna  路  3Comments

HachimDev picture HachimDev  路  3Comments