表单文件域

获取文件相关信息(文件名、文件大小、文件类型)

1
2
<input type="file" name="singlefile">
<input type="file" name="multipleFiles" multiple>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var singlefile = document.querySelector('[name=singlefile]');
singlefile.addEventListener('change', function () {
var file = this.files[0];
console.log(file.name +'\n',file.size +'\n',file.type);
},false);
var multipleFiles = document.querySelector('[name=multipleFiles]');
multipleFiles.addEventListener('change', function () {
var len = this.files.length;
var file;
for (var i = 0; i < len; i++) {
file = this.files[i];
console.log(file.name +'\n',file.size +'\n',file.type);
}
},false);