Home > Web Front-end > JS Tutorial > body text

JavaScript determines the file type uploaded by the FileUpload control_javascript skills

WBOY
Release: 2016-05-16 15:37:49
Original
1473 people have browsed it

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);
Copy after login

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>
Copy after login

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.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!