文件过大错误处理Node.js Express Multer [英] File Too Large Error Handling Nodejs Express Multer

查看:479
本文介绍了文件过大错误处理Node.js Express Multer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ExpressJS中使用multer。我想将文件上传限制为0.5 MB。

I am using multer with ExpressJS. I want to limit file uploads to 0.5 MB.

var limits = { fileSize: 0.5 * 1024 * 1024 };

var upload = multer({ dest: './public/uploads/', limits: limits
 }).single('upl')

 app.post('/',upload, function (req, res) {
  upload(req, res, function (err) {
    if (err) {
            console.log("cs " + err);
      return
    }

           res.end('You new avatar is uploaded')
    // Everything went fine
  })
})

文件小于0.5 MB时,我会看到您的新头像是上传,但结束后,我得到

When the file is under 0.5 MB I can see 'You new avatar is uploaded' but when it is over I get,

Error: File too large
   at makeError (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-error.js:12:13)
   at abortWithCode (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-middleware.js:77:22)
   at FileStream.<anonymous> (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-middleware.js:139:11)
   at emitNone (events.js:80:13)
   at FileStream.emit (events.js:179:7)
   at PartStream.onData (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\busboy\lib\types\multipart.js:220:18)
   at emitOne (events.js:90:13)
   at PartStream.emit (events.js:182:7)
   at readableAddChunk (_stream_readable.js:153:18)
   at PartStream.Readable.push (_stream_readable.js:111:10)

很显然,我永远也不想让最终用户看到这一点。我如何正确处理呢?

Obviously I would never want the end user to see this. How can I handle this properly?

推荐答案

尝试这样重写(先删除上传):

try rewrite like this (remove "upload" first):

app.post('/', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
        console.log("cs " + err);
      return
    }

    res.end('You new avatar is uploaded')
    // Everything went fine
  })

这篇关于文件过大错误处理Node.js Express Multer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆