req.files.upload.length返回文件计数为零,或上传多个文件,当一个文件上传时,它返回文件大小
In this loop, “req.files.upload.length” returns file count when 0, or more than one files are uploaded, however when one file is uploaded “req.files.upload.length” returns file size. Why?
在此循环中,“req.files.upload.length”在0或多个文件上载时返回文件计数,但是当上载一个文件时,“req.files.upload.length”返回文件大小。为什么?
upload handler:
上传处理程序
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');
});
When I upload one file I get this error:
当我上传一个文件时,我收到此错误:
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
1 个解决方案
#1
3
In Express/Connect, req.files
will be an object with the input[type=file]
name
attribute as key, and the content as the value. If there are more than one input[type=file]
with the same name
, it will be an array instead. If you control the view, i.e. the web page with the form
, you should be able to have either unique names for each file input, or know that you have more than one with the name "upload".
在Express / Connect中,req.files将是一个对象,其输入[type = file] name属性为键,内容为值。如果有多个输入[type = file]具有相同的名称,则它将是一个数组。如果您控制视图,即带有表单的网页,您应该能够为每个文件输入设置唯一的名称,或者知道您有多个名称为“upload”的名称。
Update: I didn't think of the most obvious case, where the user has selected more than one file.
更新:我没有想到最明显的情况,用户选择了多个文件。
Update: Here's what req.files
will look like for two different cases:
更新:以下是两种不同情况下req.files的样子:
One file:
一个文件:
req.files = { upload: { name: 'foo.txt' length: 1024 } }
Two files:
两个文件:
req.files = { upload: [ { name: 'foo.txt' length: 1024 }, { name: 'bar.txt' length: 2048 } ] }
As should be clear from this example, req.files.upload
will have a length
property in either case, but as you have found out they mean different things.
从这个例子中可以清楚地看出,req.files.upload在任何一种情况下都会有一个length属性,但是你发现它们意味着不同的东西。
You probably want to iterate over an array in any case, to simplify your own code. This is easy to accomplish with [].concat
:
您可能希望在任何情况下迭代数组,以简化您自己的代码。使用[] .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];
// ...
}
更多相关文章
- 如何.abort()ajax文件上传?
- 错误地将JSON数据写入文件。
- 前端文件上传原理
- 浏览器独立文件io在javascript中
- 上传文件进度条(笔记)
- 在多个文件中需要相同的模块
- 两个svg文件用javascript合并的问题?
- 将JavaScript命名空间拆分为多个文件
- 是否可以知道文件是否在用户的浏览器缓存中?