12

I am using multer to save the file on server developed through express & nodejs.

I am usign following code.

var express = require('express'),
    multer  = require('multer')

var app = express()

app.get('/', function(req, res){
  res.send('hello world');
});

app.post('/upload',[ multer({ dest: './uploads/'}), function(req, res){

    res.status(204).end()
}]);

app.listen(3000);

Multer saves the file for me in the specified destination folder.

All this is working fine but I have following questions:

  1. If the file saving fails for various reasons, it looks like my route will always return status 204.
  2. I am not sure if status 204 is retured after file is saved or while the file is getting saved asynchronously, status 204 is returned.
4

6 回答 6

11

这是编写处理上传和错误的multer中间件的方法

const multer = require("multer");

function uploadFile(req, res, next) {
    const upload = multer().single('yourFileNameHere');

    upload(req, res, function (err) {
        if (err instanceof multer.MulterError) {
            // A Multer error occurred when uploading.
        } else if (err) {
            // An unknown error occurred when uploading.
        }
        // Everything went fine. 
        next()
    })
}
于 2019-10-22T00:41:02.927 回答
10

onError您可以使用以下选项处理错误:

app.post('/upload',[
  multer({
    dest    : './uploads/',
    onError : function(err, next) {
      console.log('error', err);
      next(err);
    }
  }),
  function(req, res) {
    res.status(204).end();
  }
]);

如果您调用next(err),您的路由处理程序(生成 204)将被跳过,错误将由 Express 处理。

认为(不是 100% 确定,因为它取决于multer实现方式)在保存文件时将调用您的路由处理程序。您可以onFileUploadComplete在上传完成后记录一条消息,并将其与调用路由处理程序的时间进行比较。

查看代码,multer当文件完全上传时调用下一个中间件/路由处理程序。

于 2015-06-15T07:11:35.710 回答
7

尝试这个

var upload = multer().single('avatar')

app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      // An error occurred when uploading 
      return
    }

    // Everything went fine 
  })
}

参考:

http://wiki.workassis.com/nodejs-express-get-post-multipart-request-handling-example/

https://www.npmjs.com/package/multer#error-handling

于 2016-08-19T09:34:33.200 回答
0

从下面的代码(muter index.js 文件的源代码)中可以看出,如果您不传递onError回调,错误将由 express 处理。

    fileStream.on('error', function(error) {
      // trigger "file error" event
      if (options.onError) { options.onError(error, next); }
      else next(error);
    });
于 2015-11-20T18:53:38.067 回答
0
var multer = require('multer')
var upload = multer().single('avatar')
 
app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err instanceof multer.MulterError) {
      handle error
    } else if (err) {
      handle error
    }
    else{
      write you code
    }
  })
})


you can see this from the [documentation][1]


  [1]: https://www.npmjs.com/package/multer#error-handling
于 2021-02-13T03:36:35.903 回答
0

当用户向您发送任何内容时,请注意系统

我通常设置更多[*option1]:

process.on('uncaughtException', function(ls){
  // console.log(ls);
  (function(){})();
});

接着:

var upload= multer({ dest: __dirname + '/../uploads/' }).single('photos');
// middle ..
upload(req, res, function (err) {
  if (err instanceof multer.MulterError) {
    // A Multer error occurred when uploading.
    console.log('MulterError', err);
  } else if (err) {
    // An unknown error occurred when uploading.
    // Work best when have [*option1]
    console.log('UnhandledError', err);
  }
  if(err) {
    return res.sendStatus(403);
  }
  res.sendStatus(200);
});

包:“multer”:“^1.4.2”

于 2020-08-15T01:43:16.323 回答