简体   繁体   English

html5输入类型文件浏览器的区别

[英]html5 input type file browser differences

I have an issue with Firefox and IE11 that seems very strange. 我的Firefox和IE11有一个问题,看起来很奇怪。 I want to use input type=file to get a fileList, it works in chrome and Safari, the value returns the filename, but fileList is empty in FireFox and IE 我想使用输入type = file来获取fileList,它在chrome和Safari中工作,该值返回文件名,但是在FireFox和IE中fileList为空

here's the html, just put it in a file and open with a browser, or put it on a website and load the page. 这是html,只需将其放在文件中并使用浏览器打开,或者将其放在网站上并加载页面即可。

<html>
<head>
<title>input file test</title>
<script>
function file1_click(){
"use strict";    
var f = document.getElementById('file1');
alert('value=' + f.value + '\nfiles=' + JSON.stringify(f.files) );
}   
</script>
</head>
<body>
<form id="form1">
<input type="file" id="file1" onchange="file1_click();" /> 
</form>
</body>
</html>

f.files is an object. f.files是一个对象。 You can loop through it. 您可以遍历它。

function file1_click(){
    "use strict";    
    var f = document.getElementById('file1');
    var filesArray = [];
    for(var i=0, len=f.files.length; i<len; i++) {
        filesArray.push(f.files[i]['name'])
    }
    alert('value=' + f.value + '\nfiles=' + JSON.stringify(filesArray) );
}

Additionally, you will need to specify the "multiple" property on your file input if you want more than one image. 此外,如果您需要多个图像,则需要在文件输入中指定“多个”属性。

<input type="file" id="file1" onchange="file1_click();" multiple />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM