'Create API for upload file to zoho catalyst using node js

I am creating API for upload file to Zoho Catalyst using Zoho Catalyst dependancy but i got an error that request body is wrong format.

expressApp.post('/uploadFile', upload.single('file'), (req, res) => {
(async () => {
    try {
        if (typeof req.file != 'undefined' && typeof req.body.folderId != 'undefined') {
            var app = catalyst.initialize(req);
            let filestore = app.filestore();
            let folder = filestore.folder(req.body.folderId);
            const stream = Readable.from(req.file.buffer);
            var data = {
                code: stream,
                name: req.file.originalname
            };
            console.log(data);
            var result = await folder.uploadFile(data);
            return res.status(200).json(successResponse(result));
        }
        return res.status(200).json(errorResponse('Required parameter is missing!'));
    } catch (error) {
        return res.status(500).json(errorResponse(error));
    }
  })();
});

Following is example code from zoho Documentation to pass parameter

let config = {
    code: fs.createReadStream('empdata.csv'),
    name: 'testFile.txt'
}
folder.uploadFile(config);

Please help me how to process file directly to Zoho catalyst. Thanks.



Solution 1:[1]

Uploading files in Nodejs usually requires a middleware like multer, busboy or express-fileupload. You can find the code sample to get started with file uploads, deleting files, etc right here. Attaching the sample code for file upload for your reference.

app.post('/uploadFile', async (req, res) => {
try {
    const app = catalyst.initialize(req);
    await req.files.data.mv(`${__dirname}/uploads/${req.files.data.name}`);
    await app.filestore().folder(FOLDER_ID).uploadFile({
        code: fs.createReadStream(`${__dirname}/uploads/${req.files.data.name}`),
        name: req.files.data.name
    });
    res.status(200).send({
        "status": "success",
        "message": "File Uploaded Successfully"
    })
} catch (error) {
    res.status(500).send({
        "status": "Internal Server Error",
        "message": error
    })
}});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SRIVIDHYA SUBRAMANIAN