0

在这个循环中,“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    
4

1 回答 1

3

在 Express/Connect 中,req.files将以input[type=file] name属性为键,内容为值的对象。如果有多个input[type=file]相同的name,它将是一个数组。如果您控制视图,即带有 的网页form,您应该能够为每个文件输入拥有唯一的名称,或者知道您有多个名称为“upload”。

更新:我没有想到最明显的情况,即用户选择了多个文件。

更新:以下是req.files两种不同情况的情况:

  1. 一个文件:

    req.files = {
      upload: {
        name: 'foo.txt'
        length: 1024
      }
    }
    
  2. 两个文件:

    req.files = {
      upload: [
        {
          name: 'foo.txt'
          length: 1024
        },
        {
          name: 'bar.txt'
          length: 2048
        }
      ]
    }
    

从这个例子中应该很清楚,在任何一种情况下req.files.upload都会有一个length属性,但是你已经发现它们意味着不同的东西。

在任何情况下,您都可能希望遍历数组,以简化您自己的代码。这很容易实现[].concat

// Make sure that files is always an array
var files = [].concat(req.files.upload);
for(var x = 0; x < files.length; x++){
    file = files[x];
    // ...
}
于 2012-05-29T12:23:11.440 回答