关于javascript:js-截取文件后缀名

35次阅读

共计 638 个字符,预计需要花费 2 分钟才能阅读完成。

办法一:

<input type="file" onChange="change(event)">
<script>
function change(event) {var fileName = event.target.files[0].name.toLowerCase();
    var lastIndex = fileName.lastIndexOf('.');
    var suffix = fileName.substring(lastIndex, fileName.length);
    console.log(suffix);
}
</script>

办法二:

<input type="file" onChange="change(event)">
<script>
function change(event) {var fileName = event.target.files[0].name.toLowerCase();
    var suffix = fileName.split('.').pop();
    console.log(suffix);
}
</script>

办法三:

<input type="file" onChange="change(event)">
<script>
function change(event) {var fileName = event.target.files[0].name.toLowerCase();
    var suffix = fileName.match(/.[^.]+$/)[0];
    console.log(suffix);
}

正文完
 0