ie10以下怎么获取文件大小

ie

  ie10以下没有files对象,因此不能通过files来获取选中文件的大小。

解决办法

  ie8以上有效:

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

 获取文件名

  直接 对象.value 获取文件地址加文件名

 

猜你喜欢

转载自www.cnblogs.com/GH0522/p/10197504.html
今日推荐