js判斷上傳檔案尾碼名是否合法,js上傳檔案尾碼

來源:互聯網
上載者:User

js判斷上傳檔案尾碼名是否合法,js上傳檔案尾碼

本文執行個體介紹了js判斷上傳檔案尾碼名是否合法的詳細代碼,分享給大家供大家參考,具體內容如下

選擇檔案

選擇1.jpg檔案

上傳成功合法

除圖片檔案格式以外,其他都是不合法的操作。

具體代碼:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>判斷檔案上傳是否合法</title><script type="text/javascript">//3、(字元)檢查檔案上傳表單控制項,如果含有[jpg,jpeg,gif,png]則顯示“檔案類型合法”,否則“顯示檔案類型錯誤”function checkFileExt(filename){ var flag = false; //狀態 var arr = ["jpg","png","gif"]; //取出上傳檔案的副檔名 var index = filename.lastIndexOf("."); var ext = filename.substr(index+1); //迴圈比較 for(var i=0;i<arr.length;i++) {  if(ext == arr[i])  {   flag = true; //一旦找到合適的,立即退出迴圈   break;  } } //條件判斷 if(flag) {  document.write("檔案名稱合法"); }else {  document.write("檔案名稱不合法"); }}</script></head><body><form name="form1" method="post" enctype="multipart/form-data">上傳頭像:<input type="file" name="uploadFile" onchange="checkFileExt(this.value)" /></form><input type="button" value="返回上一頁" onclick="javascript:history.go(-1)" /></body></html>

希望本文所述對大家學習javascript程式設計有所協助。

您可能感興趣的文章:
  • javascript 得到檔案尾碼名的思路及實現
  • js擷取判斷上傳檔案尾碼名的範例程式碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.