Last active
August 28, 2023 12:35
-
-
Save tonkla/5e893aa8776923ad6a2c9c6b7c432f3d to your computer and use it in GitHub Desktop.
Parsing 'multipart/form-data' Excel (.xlsx) with Busboy on AWS Lambda (Node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const Busboy = require('busboy') | |
const XLSX = require('xlsx') | |
function parseMultipartFormData(input, contentType) { | |
return new Promise((resolve, reject) => { | |
const buffers = [] | |
const busboy = new Busboy({ | |
headers: { 'content-type': contentType }, | |
}) | |
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { | |
file.on('data', data => { | |
buffers.push(data); | |
}) | |
file.on('end', () => { | |
resolve(Buffer.concat(buffers)); | |
}) | |
}) | |
busboy.on('error', error => reject(error)) | |
busboy.end(input) | |
}) | |
} | |
module.exports.upload = async (event, context) => { | |
try { | |
const contentType = event.headers['content-type'] || event.headers['Content-Type'] | |
const data = await parseMultipartFormData(event.body, contentType) | |
const workbook = XLSX.read(data, { type: 'buffer' }) | |
const jsonRows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]) | |
console.log(jsonRows) | |
} catch (error) { | |
console.log(error) | |
} | |
} |
I m also getting same actually, Please help on this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me this returns in logs
WORKBOOK!! { SheetNames: [ 'Sheet1' ], Sheets: { Sheet1: { '!ref': 'A1' } } }
JSONROWS []
I have multiple sheets but this just displays Sheet 1. Why is it empty? How do I see the data in my sheets in JSON object form?