我正在将图像文件上传到我的后端,并且收到一个错误,称有效负载过大。我增加了文件大小的上限,该文件大小可以使主体解析到远高于标准图像大小的位置,但是仍然出现错误。感谢所有帮助。干杯!
服务器设置
// parse application/json
app.use(bodyParser.json({limit: '100mb', extended: true}))
app.use(bodyParser.urlencoded({limit: '100mb', extended: true}))
// General setup
app.use( function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
POST方法
app.post('/uploadProfilePicture', upload.single('im-a-file'), function (req, res, next) {
try {
let file = req.body;
let id = '' + req.query.uid + '/profilePicture/' + file.name;
/* Handle image ... */
res.send('Complete!')
next();
} catch (e) {
console.log('Found error: ' + e)
res.send(new Error(e));
}
})
错误
PayloadTooLargeError: request entity too large
at readStream (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/raw-body/index.js:155:17)
at getRawBody (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/raw-body/index.js:108:12)
at read (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/body-parser/lib/read.js:77:3)
at urlencodedParser (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/body-parser/lib/types/urlencoded.js:116:5)
at Layer.handle [as handle_request] (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/router/index.js:317:13)
at /Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/router/index.js:335:12)
at next (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/router/index.js:275:10)
at expressInit (/Users/Ryan/Desktop/School/Nonprofit/Project/backend/node_modules/express/lib/middleware/init.js:40:5)
请注意,该方法是POST(正文将是内容),但也具有UID参数。