NodeJS:'multer'文件上传回调函数

时间:2017-04-24 08:25:47

标签: node.js multer

我正在使用'multer'插件进行文件上传。我想在文件上传成功后调用另一个函数。

这是我的代码:

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 need to call this function after image fully uploaded
var uploadSuccessFn = function () {
    //code 
}

5 个答案:

答案 0 :(得分:1)

当你调用upload的进程时,函数(进程内)是回调函数只是使用它。你写的是console.log行,这意味着你的文件上传但你没有使用时会调用你的回调功能..

可以这样做: -

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

它会给你json的回复。

答案 1 :(得分:1)

您可以更改代码以在POST处理程序中使用2个函数:

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请求处理程序中使用它:

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

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

.
.

答案 2 :(得分: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')才能使其正常工作。希望这有帮助。

答案 3 :(得分:0)

在您调用upload的代码中,输入if条件来检查回调响应。如果响应为null,则可以在该条件下调用另一个函数,否则会显示一些错误。

我猜您尝试调用的函数会向用户显示某种成功消息。

答案 4 :(得分:0)

试试这个

    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
    }