js file size is determined using the img dynsrc, and more compatible with IE10

Description link

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

Guess you like

Origin www.cnblogs.com/sunny3158/p/12181852.html