The example in this article describes how js implements browsing local files and displaying extensions. Share it with everyone for your reference. The details are as follows:
Here you use the file field to browse the specified file and you can display the extension of the file. Think about where it can be used? It can be used in the file upload system to determine whether the uploaded file type is a legal type. If not, uploading is not allowed. Take another look at the code, it’s less than 10 lines, and it solves the problem. Friends who are learning JavaScript, you can also refer to it.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-view-nav-file-ext-codes/
The specific code is as follows:
<html> <head> <title>取得文件字段中选取文件的扩展名</title> <script language="javascript"> <!-- function getFilename(){ str = new Array(); filename = form1.file.value; str = filename.split("."); extname = str[str.length - 1]; alert(extname); } //--> </script> </head> <body> <form enctype="multipart/form-data" name="form1"> <input type="file" name="file"> <input type="button" value="显示扩展名" onClick="getFilename()"> </form> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.