I need to read a bunch of image files (JPEGs) from a folder using ExpressJS and return each image as a base64 encoded data using an array. So the end result should be an array of base64 objects. I’ve below code but it’s not working as I get the below error..
data.getListOfImages = async function (res, next) {
let retVal = { success: true };
try {
fs.readdir('./images/', function (err, files) {
if (err) {
throw new Error(err);
}
files.forEach(file => {
fs.readFile('./images/'+file, {encoding: 'base64'}, function(err, data) {
if (err) throw err // Fail if the file can't be read.
cardList[cntr]=data
logger.info(file);
cntr++
})
});
res.writeHead(200, {'Content-Type': 'image/jpeg:base64'})
res.end(cardList);
next(null, { success: true, error: null, result: res });
})
}
catch (error) {
logger.info(
"addJob ==> Error while extracting image files : " + error
);
retVal = { success: false, error: error };
next(error, null);
}
};
I get below error ….
"error":{"code":"ERR_INVALID_ARG_TYPE"},"exception":true,"level":"error","message":"uncaughtException: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArraynTypeError [ERR_INVALID_ARG_TYPE]
any idea how to achieve this?