• 通过input上传图片,判断不同浏览器及图片类型和大小的js代码


    1.jsp页面代码

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <form id="userPhoto" name="userPhoto" method="post" action="../uploadUserPhoto" enctype="multipart/form-data">  
    2.     <input type="hidden" id="max_PhotoSize" name="maxPhotoSize" value="${maxPhotoSize}"/>  
    3.     <input type="hidden" id="photoType" name="photoType" value=""/>  
    4.     <input type="file" id="filePhoto"  value=""   
    5.         style="font-size:400px;opacity:0;filter:alpha(opacity:0);-moz-opacity:0;position:absolute;top:0px;left:0px;400px;height:400px;z-index:100;overflow:hidden;cursor:pointer;"   
    6.         name="userPhoto"/>  
    7.         <img style="top:0px;left:0px;z-index:99" id="imgUserPhoto" src="" width="100" height="140">  
    8.         <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0" align="center">  
    9.             <tr>  
    10.                 <td width="100%" align="center" valign="middle">  
    11.                 <s:hidden id="userId" name="user.userId" />  
    12.                 </td>  
    13.             </tr>  
    14.         </table>  
    15.     </form>  


    2.js代码

    [javascript] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. function uploadImage() {  
    2.   
    3.         var fileSize = 1024 * 5000;  
    4.         var maxFileSize= $("#max_PhotoSize")[0].value ;  
    5.         var location = $("#filePhoto")[0].value;  
    6.         var point = location.lastIndexOf(".");  
    7.         var type = location.substr(point);  
    8.         $("#photoType").val(type);  
    9.         if (type == ".jpg" || type == ".gif" || type == ".JPG" || type == ".GIF" || type == ".bmp" || type == ".BMP"   
    10.             || type == ".jpeg" || type == ".JPEG" || type == ".PNG" || type == ".png" ) {  
    11.   
    12.             if ($.browser.msie) {//IE  
    13.                 var img = document.createElement("img");              
    14.                 img.src = $("#filePhoto")[0].value;  
    15.                 fileSize = img.fileSize;                  
    16.             }  
    17.             if ($.browser.mozilla || (navigator.userAgent.toLowerCase().match(/chrome/) !=null)) {//火狐或者chrome  
    18.                 fileSize = $("#filePhoto")[0].files[0].size;  
    19.             }  
    20.             if (fileSize > maxFileSize*1024) {  
    21.   
    22.                 alert("图片尺寸请不要大于"+maxFileSize+"KB");  
    23.                 return false;  
    24.             } else {  
    25.                 $("#userPhoto")[0].submit();  
    26.                 return true;  
    27.             }  
    28.         } else {  
    29.             alert("只能上传jpg,gif,bmp,jpeg,png格式的图片");  
    30.             return false;  
    31.         }  
    32.         return true;  
    33.   
    34.     }  



    3.补充:

    以上关于IE的验证,适合于IE6以前的版本,对于IE7以上的版本前台不好判断图片的大小,最好还是通过后台判断:

    (1)先将Form提交

    (2)在后台判断提交图片文件大小,如果大于最大限制,则不做任何处理,返回提示语,如果在最大限制内,则再进行保存等处理

  • 相关阅读:
    ES的基本用法
    Elasticsearch安装部署
    jquery-layer弹框在火狐浏览器中弹框不显示的问题
    MySQL中横表和竖表相互转换
    echarts--迁徙图特性简介
    Lucene入门案例一
    Lucene入门简介
    SpringMVC中的拦截器
    SpringMVC中Json数据格式转换
    SpringMVC中的文件上传
  • 原文地址:https://www.cnblogs.com/keyi/p/6134343.html
Copyright © 2020-2023  润新知