Node File upload issue

Viewed 45

I am constantly getting the printed out message of "No File Upload" Failed when I select my image and hit upload. It never goes to true..

As you can see, I am not actually uploading here. Just testing the req.files is there something wrong in my router.post? Any input would be appreciated.

router.post('/upload', async (req, res) => {

 
  try {
        if(!req.files) {
            res.send({
                status: false,
                message: 'No file uploaded'
            });
        } else {
            res.send({
                status: true,
                message: 'Files are uploaded',
                data: data
            });
        }
    } catch (err) {
        res.status(500).send(err);
    }


})
module.exports=router

2 Answers

can you share your controller file of it and also you are using async function but not defining await inside of that function,must have to use 'await' when executing async function.

here is the controller file AAmir

let fs = require('fs');
let async = require('async');

function uploaddownFiles(connection, fromFolder, toFolder, sftpmethod) {
    return new Promise((resolve, reject) => {
        // Getting all file list in given folder of local machine
        let fileList = fs.readdirSync(fromFolder);
        // filter only files not folders
        fileList = fileList.filter(file => {
            if (file.includes('.')) return true;
            return false;
        });
        console.log('Total files: ', fileList.length)
        if (!fileList.length) return reject('No file to send')
        connection.sftp(function (err, sftp) {
            if (err) return;
            async.eachOfSeries(fileList, (file, key, cb) => {
                let moveFrom = `${fromFolder}/${file}`;
                let moveTo = `${toFolder}/${file}`;
                if (sftpmethod=== 'put')
                    sftp.fastPut(moveFrom, moveTo, {}, function (uploadError) {
                        if (uploadError) return cb(uploadError);
                        console.log("Successfully Uploaded", file);
                        cb();
                    });
                else if (sftpmethod === 'get')
                    sftp.fastGet(moveFrom, moveTo, {}, function (uploadError) {
                        if (uploadError) return cb(uploadError);
                        console.log("Successfully Downloaded", file);
                        cb();
                    });
            }, function (err) {
                if (err) {
                    console.log(err);
                    return reject(err);
                } else {
                    console.log('all files have been uploaded/downloaded');
                    return resolve();
                }
            })
        });
    });
}

Related