Sankalp Sankalp - 1 month ago 10
Node.js Question

NPM Multer returning no response

I am trying to upload a file using

npm multer
. The issue is multer is returning no response, not even error. I tried to create the common function for whole application for file upload. I have all (777) permission to my folder as well.

My sample code is.

var uploadFile = require('../../utilities/common').uploadFile;
exports.uploadVehicle = function (req, res) {
console.log('\nUploading File');
console.log(dirPath.vehicleFarm);
uploadFile(dirPath.vehicleFarm, 'csvFile', function (err) {
console.log('\tError:');
console.log(err);
if (err)
return res.status(err.status || 400).json({code: err.status || 400, message: err.message || 'Unable to process request.'});
var data = req.body;
console.log(data.csvFile);
res.status(200).json({status: 200, message: 'File saved successfully'});
});
};


The
uploadFile
function in common file is mentioned like this.

var multer = require('multer');
var path = require('path');
var fs = require('fs');
var filePath = path.resolve(__dirname, '..');
var storage = function (inputDir, fieldName) {
console.log('Input from storage to path - ' + inputDir);
console.log(filePath + '/uploads/' + inputDir);
return multer.diskStorage({
destination: function (req, file, cb) {
cb(null, filePath + '/uploads/' + inputDir);
},
filename : function (req, file, cb) {
var dateTimeStamp = Date.now();
req.body[fieldName] = file.fieldname + '-' + dateTimeStamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1];
console.log(req.body[fieldName]);
cb(null, req.body[fieldName]);
}
});
};
exports.uploadFile = function (inputDir, fieldName) {
return multer({storage: storage(inputDir, fieldName)}).single(fieldName);
};


Input path is also valid even the folder also exists Path is -
/home/sony/Desktop/node/all/db/uploads/tempCSV/vehicleFarm/


Where I have mistaken? How would I trace if file uploads fails as well?

Answer

Since with target to create single function with dynamic parameters for directory and filename. Here the issue is resolved.

var dirPath                  = require('./directory-path').dirPath;
var multer                   = require('multer');
var path                     = require('path');
var fs                       = require('fs');
var filePath                 = path.resolve(__dirname, '..');
exports.uploadFileToPath     = function (directory, fieldName) {
    this.uploadFile = multer({
        storage: multer.diskStorage({
            destination: function (req, file, cb) {
                console.log('In Destination - ');
                cb(null, filePath + '/uploads/' + directory);
            },
            filename   : function (req, file, cb) {
                var dateTimeStamp   = Date.now();
                req.body[fieldName] = file.fieldname + '-' + dateTimeStamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1];
                console.log(req.body[fieldName]);
                cb(null, req.body[fieldName]);
            }
        })
    }).single(fieldName);
};

Where I need to call this function, I instantiated it like

var common     = require('../../utilities/common');

var uploadVehicle = new common.uploadFileToPath(dirPath.vehicleFarm, 'csvFile');
uploadVehicle.uploadFile(req, res, function (err) {
    if (err) {
        console.log('\tError:');
        console.log(err);
        return res.status(err.status || 400).json({code: err.status || 400, message: err.message || 'Unable to process request.'});
    }
    var data = req.body;
    console.log('File Name --- ' + data.csvFile);
    //if (data.length === 0)
    //  return callback({status: 400, message: 'Bad request. No record found.'});
    res.status(200).json({status: 200, message: 'File saved successfully'});
});

Now its properly returning response in error case also. Files are uploading to dynamic path passed in params.

Comments