chrome 66下setTimeout回调中触发<input type=file>的click事件,无法弹出文件选择框?

2,801 阅读1分钟

问题描述

chrome 66,在setTimeout回调中触发type为file的input框的点击事件,文件选择框是否弹出和时长有关

操作系统:windows 7

浏览器:chrome 66

1000ms以上:上传的文件框未弹出

1000ms以下:上传的文件框弹出


操作系统:windows 7

浏览器:chrome 75

5000ms以上:上传的文件框未弹出,控制台有提示File chooser dialog can only be shown with a user activation

5000ms以下:上传的文件框弹出

相关代码

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>

<div class="button">
    <button onclick="clickBtn()">请选择文件</button>
</div>
<div class="input" style="display: none;">
    <input type="file" id="fileInput">
</div>
<script>
    var clickBtn = function() {
        setTimeout(function() {
            console.log('input click')
            document.querySelector('#fileInput').click();
        }, 1000);
    }
</script>
</body>
</html>

疑问

1.问什么会因为时长不一样,文件选择框弹出的表现不一致

2.chrome 75,同样都是模拟用户点击,为什么会因为时长不一样,5000ms以上就会打印提示File chooser dialog can only be shown with a user activation