sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

原文链接

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. <head>
  3. <meta name="DEscription" contect="my code demo" />
  4. <meta name="Author" contect="Michael@jb51.net" />
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta http-equiv="X-UA-Compatible" content="IE=emulateIE8">
  7. <title>js check file size @ jb51.net</title>
  8. </head>
  9. <body>
  10. < img id="tempimg" dynsrc="" src="" style="display:none" />
  11. <input type="file" name="file" id="fileuploade" size="40" />
  12. <input type="button" name ="check" value="checkfilesize" οnclick="checkfile()"/>
  13. </body>
  14. <script type="text/javascript">
  15. var maxsize = 210241024;//2M
  16. var errMsg = "上传的附件文件不能超过2M!!!";
  17. var tipMsg = "您的浏览器暂不支持计算上传文件的大小,确保上传文件不要超过2M,建议使用IE、FireFox、Chrome浏览器。";
  18. var browserCfg = {};
  19. var ua = window.navigator.userAgent;
  20. if (ua.indexOf("MSIE")>=1){
  21. browserCfg.ie = true;
  22. }else if(ua.indexOf("Firefox")>=1){
  23. browserCfg.firefox = true;
  24. }else if(ua.indexOf("Chrome")>=1){
  25. browserCfg.chrome = true;
  26. }
  27. function checkfile(){
  28. try{
  29. var obj_file = document.getElementById("fileuploade");
  30. if(obj_file.value""){
  31. alert("请先选择上传文件");
  32. return;
  33. }
  34. var filesize = 0;
  35. if(browserCfg.firefox || browserCfg.chrome ){
  36. filesize = obj_file.files[0].size;
  37. }else if(browserCfg.ie){
  38. var obj_img = document.getElementById('tempimg');
  39. obj_img.dynsrc=obj_file.value;
  40. filesize = obj_img.fileSize;
  41. alert(filesize);
  42. }else{
  43. alert(tipMsg);
  44. return;
  45. }
  46. if(filesize-1){
  47. alert(tipMsg);
  48. return;
  49. }else if(filesize>maxsize){
  50. alert(errMsg);
  51. return;
  52. }else{
  53. alert("文件大小符合要求");
  54. alert(filesize);
  55. return;
  56. }
  57. }catch(e){
  58. alert(e);
  59. }
  60. }
  61. </script>
  62. </html>

posted on 2020-01-12 07:59  sunny123456  阅读(315)  评论(0编辑  收藏  举报