NodeJS: file upload callback function "multer"

I use the 'multer' plugin to upload files. I want to call another function after a successful file upload.

Here is my code:

module.exports.uploadFile = upload.single('file', '_id'), function (req, res, next) {
    console.log('Uploade Successful');
}

var upload = multer({
storage: multer.diskStorage({
    destination: './Media/ChatDocUpload',
    filename: function (req, file, cb) {
        var dest = './Media/ChatDocUpload';

        //query string params
        var _chatMessageID = req.query.chatMessageID;

        var _ext = file.originalname.substring(file.originalname.indexOf("."));
        var _fileName = _chatMessageID + _ext;

        cb(null, _fileName);
    }
})
});

I want to call my new function after loading the image. Using this code, I can load the image successfully, but not get the callback function.

I need to call a new function after image upload is complete.

//I need to call this function after image fully uploaded
var uploadSuccessFn = function () {
    //code 
}
+6
source share
5 answers

( ), upload, , . console.log, , , .

: -

function(req,res){  //callback function
res.json({message:"file uploaded successfully"});
}

json.

+1

, , 2 POST:

module.exports = {uploadFile: uploadFile, afterUpload: afterUpload};

function uploadFile(){
    upload.single('file', '_id');
}

function afterUpload(req, res, next) {
        console.log('Uploade Successful');
    }


    var upload = multer({
    storage: multer.diskStorage({
        destination: './Media/ChatDocUpload',
        filename: function (req, file, cb) {
            var dest = './Media/ChatDocUpload';

            //query string params
            var _chatMessageID = req.query.chatMessageID;

            var _ext = file.originalname.substring(file.originalname.indexOf("."));
            var _fileName = _chatMessageID + _ext;

            cb(null, _fileName);
        }
    })
    });

post post:

.
.
var imageUpload = require('./handler');

app.post('/image/upload',imageUpload.uploadFile,imageUpload.afterUpload);

.
.
+1

multer:

Multer Storage Engine

. - :

MyCustomStorage.prototype._handleFile = function _handleFile (req, file, cb) {
  this.getDestination(req, file, function (err, path) {
    if (err) return cb(err)

  var outStream = fs.createWriteStream(path)

  file.stream.pipe(outStream)
  outStream.on('error', cb)
  outStream.on('finish', function () {

  // Insert here whatever you want to do...
    console.log('Successfully Uploaded');

  cb(null, {
    path: path,
    size: outStream.bytesWritten
  })
}) })}

, var fs = require ('fs'), . , .

+1

In your code where you invoke the load, set an if condition that checks the callback check. If the answer is null, then you can call another function in this state, and also show some error.

I think that the function you are trying to call will show the user some message about success.

0
source

try it

    var storage = multer.diskStorage({
      destination: function (req, file, cb) {
        return cb(null, './Media/ChatDocUpload')
      },
      filename: function (req, file, cb) {
        var _chatMessageID = req.query.chatMessageID;
        var _ext = file.originalname.substring(file.originalname.indexOf("."));
        var _fileName = _chatMessageID + _ext;
         return cb(null, _fileName);
      }
    })

    var upload = multer({ storage: storage })

    module.exports.uploadFile = upload.single('file'), function (req, res, next) {
        console.log('Uploade Successful');
// you function after uploading images
    }
0
source

Source: https://habr.com/ru/post/1016716/


All Articles