在这个循环中,“req.files.upload.length”在 0 时返回文件计数,或者上传多个文件,但是当上传一个文件时,“req.files.upload.length”返回文件大小。为什么?
上传处理程序:
app.post('/upload', function(req, res){
console.log('file count: ' + req.files.upload.length);
var file;
var i = 0;
for(var x = 0; x < req.files.upload.length; x++){
file = req.files.upload[x];
console.log(file.name + ' (' + (file.size * 0.0009765625).toFixed(0) + ' kB, ' + file.type + ')');
};
console.log(req.files.upload.length + ' files uploaded to server');
res.redirect('/forms');
});
当我上传一个文件时,我收到此错误:
TypeError: Cannot read property 'name' of undefined
at /Users/frode/Dropbox/Nettsider/expressmal/app.js:296:20
at callbacks (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:272:11)
at param (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:246:11)
at pass (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:45:10)
at Context.next (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/node_modules/connect/lib/http.js:204:15)
at Context.<anonymous> (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/passport/lib/passport/context/http/actions.js:64:8)
at SessionStrategy.pass (native)
at /Users/frode/Dropbox/Nettsider/expressmal/node_modules/passport/lib/passport/strategies/session.js:48:12
URIError: URI malformed
at /Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/node_modules/connect/lib/middleware/static.js:119:14
at Object.static [as handle] (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/node_modules/connect/lib/middleware/static.js:60:5)
at next (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/node_modules/connect/lib/http.js:204:15)
at pass (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:219:24)
at Router._dispatch (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/lib/router/index.js:45:10)
at Context.next (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/express/node_modules/connect/lib/http.js:204:15)
at Context.<anonymous> (/Users/frode/Dropbox/Nettsider/expressmal/node_modules/passport/lib/passport/context/http/actions.js:64:8)
at SessionStrategy.pass (native)
at /Users/frode/Dropbox/Nettsider/expressmal/node_modules/passport/lib/passport/strategies/session.js:48:12