+
80
-

如何获取html中的input file选择的文件名?

请问如何获取html中的input file选择的文件名?

网友回复

+
0
-

只获取文件名 document.getElementById('file').files[0].name 获取带路径的文件名 document.getElementById('file').value

示例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <input type="file" id="file"><br>

    <script type="text/javascript">
        document.querySelector("#file").addEventListener("change", function () {
            //只获取文件名
            alert(document.getElementById('file').files[0].name)

            //获取带路径的文件名
            //document.getElementById('file').value

        });

    </script>
</body>
</html>

我知道答案,我要回答