Skip to content

Instantly share code, notes, and snippets.

@tonkla
Last active August 28, 2023 12:35
Show Gist options
  • Save tonkla/5e893aa8776923ad6a2c9c6b7c432f3d to your computer and use it in GitHub Desktop.
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)
'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)
}
}
@bhushansinghr
Copy link

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