このループでは、「req.files.upload.length」は0の場合、または複数のファイルがアップロードされた場合にファイル数を返しますが、1つのファイルがアップロードされた場合は「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');
});
1つのファイルをアップロードすると、次のエラーが発生します。
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