In order to make the code simpler, the author uses regular expressions to obtain the file extension. If the reader does not know much about regular expressions or has never been exposed to them, please make up for it immediately! After all, it's so important that most programming languages support regular expressions.
As we all know, the method to obtain the FileUpload extension in the background:
string ext = Path.GetExtension(fu.PostedFile.FileName);
JS determines the extension of the file uploaded by the FileUpload control:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>JS判断文件类型-乐猪网</title> <script type="text/javascript"> function CheckFileUpload() { var obj = document.getElementById('fu'); if (obj.value == "") { alert("请选择要上传的文件!"); return false; } var stuff = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; if (stuff != "zip") { alert("文件类型不正确,请选择.zip文件!"); return false; } return true; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="fu" runat="server" /> <asp:Button ID="btnSave" runat="server" Text="保存" OnClientClick="return CheckFileUpload();" /> </div> </form> </body> </html>
FileUpload instance rendering:
The above is how JS determines the extension of the file uploaded by the FileUpload control. I hope it will be helpful to everyone's learning.