溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JQuery中怎么獲取驗證上傳文件大小

發布時間:2021-07-29 13:46:26 來源:億速云 閱讀:371 作者:Leah 欄目:編程語言

這篇文章給大家介紹JQuery中怎么獲取驗證上傳文件大小,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。


jquery
  1. /*

  2. *

  3. * Function to validate File size

  4. *

  5. **/

  6. function findSize(field_id)

  7. {

  8.            var fileInput = $("#"+field_id)[0];

  9.            byteSize  = fileInput.files[0].fileSize;

  10.         return ( Math.ceil(byteSize / 1024) ); // Size returned in KB.

  11. }

復制代碼


再把我以前用的js驗證上傳文件大小的代碼也放上吧,有需要的選擇參考。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

  2. <html xmlns="http://www.w3.org/1999/xhtml">

  3. <head>

  4. <meta name="DEscription" contect="my code demo" />

  5. <meta name="Author" contect="Michael@www.micmiu.com" />

  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  7. </head>

  8. <body>

  9.   <img id="tempimg" dynsrc="" src=""  />

  10.   <input type="file" name="file" id="fileuploade" size="40" />

  11.   <input type="button" name ="check" value="checkfilesize" onclick="checkfile()"/>

  12. </body>

  13. <script type="text/javascript">

  14.   var maxsize = 2*1024*1024;//2M

  15.   var errMsg = "上傳的附件文件不能超過2M?。?!";

  16.   var tipMsg = "您的瀏覽器暫不支持計算上傳文件的大小,確保上傳文件不要超過2M,建議使用IE、FireFox、Chrome瀏覽器。";

  17.   var  browserCfg = {};

  18.   var ua = window.navigator.userAgent;

  19.   if (ua.indexOf("MSIE")>=1){

  20.    browserCfg.ie = true;

  21.   }else if(ua.indexOf("Firefox")>=1){

  22.    browserCfg.firefox = true;

  23.   }else if(ua.indexOf("Chrome")>=1){

  24.    browserCfg.chrome = true;

  25.   }

  26.   function checkfile(){

  27.    try{

  28.      var obj_file = document.getElementById("fileuploade");

  29.      if(obj_file.value==""){

  30.       alert("請先選擇上傳文件");

  31.       return;

  32.      }

  33.      var filesize = 0;

  34.      if(browserCfg.firefox || browserCfg.chrome ){

  35.       filesize = obj_file.files[0].size;

  36.      }else if(browserCfg.ie){

  37.       var obj_img = document.getElementById('tempimg');

  38.       obj_img.dynsrc=obj_file.value;

  39.       filesize = obj_img.fileSize;

  40.      }else{

  41.       alert(tipMsg);

  42.        return;

  43.      }

  44.      if(filesize==-1){

  45.       alert(tipMsg);

  46.       return;

  47.      }else if(filesize>maxsize){

  48.       alert(errMsg);

  49.       return;

  50.     }else{

  51.      alert("文件大小符合要求");

  52.       return;

  53.     }

  54.    }catch(e){

  55.     alert(e);

  56.    }

  57.   }

  58. </script>

  59. </html>


關于JQuery中怎么獲取驗證上傳文件大小就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女