我在ExpressJS上使用Muller。我想限制文件上傳到0.5 MB。文件太大錯誤處理Nodejs Express Multer
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,我可以看到「您的新頭像上傳」但是當它在我得到的,
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)
很顯然,我會永遠不希望最終用戶看到這個。我如何正確處理這個問題?
該錯誤被返回到瀏覽器?我很驚訝。 –