做个记录
代码详细请查看原文链接
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>input file</title>
</head>
<body>
<a href="javascript:fclick();">浏览</a>
<p></p>
<input type="file"> //如果你想让用户选择多个文件,只需在 input 元素上使用 multiple 属性
<script>
document.querySelector('input').style.display = 'none';
function fclick() {
document.querySelector('p').innerHTML = "打开文件...";
document.querySelector('input').click();
}
document.querySelector('input').onchange = function () {
files = this.files[0];
if (files) {
console.log("已选定文件", files.name)
} else {
console.log('取消操作');
}
}
</script>
</body>
</html>
————————————————
版权声明:本文为CSDN博主「yxp_xa」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yxp_xa/article/details/103696863
其他js操作input file文档资料:
https://developer.mozilla.org/zh-CN/docs/Web/API/File/Using_files_from_web_applications