Using input to upload images, to determine the different browser and image type and size of the JS code

Source: Internet
Author: User

1.jsp page Code

[HTML]View PlainCopy 
  1. <form id=" Userphoto " name=" Userphoto "  method= "post"  action=enctype=" Multipart/form-data " span class= "tag" >>  
  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;width: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="   height= ">&NBSP;&NBSP;
  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 Code

[JavaScript]View PlainCopy 
  1. function Uploadimage () {
  2. var fileSize = 1024 * 5000;
  3. var maxfilesize= $ ("#max_PhotoSize") [0].value;
  4. var location = $ ("#filePhoto") [0].value;
  5. var point = Location.lastindexof (".");
  6. var type = LOCATION.SUBSTR (point);
  7. $ ("#photoType"). val (type);
  8. if (type = = ". jpg" | | type = = ". gif" | | type = = ". JPG "| | Type = = ". GIF "| | Type = = ". bmp" | | type = = ". BMP "
  9. || Type = = ". jpeg" | | type = = ". JPEG "| | Type = = ". PNG "| | Type = = ". png") {
  10. if ($.browser.msie) {//ie
  11. var img = document.createelement ("img");
  12. IMG.SRC = $ ("#filePhoto") [0].value;
  13. FileSize = img.filesize;
  14. }
  15. if ($.browser.mozilla | | (Navigator.userAgent.toLowerCase (). Match (/chrome/) =null)) {//Firefox or Chrome
  16. FileSize = $ ("#filePhoto") [0].files[0].size;
  17. }
  18. if (FileSize > maxfilesize*1024) {
  19. Alert ("picture size please do not exceed" +maxfilesize+ "KB");
  20. return false;
  21. } Else {
  22. $ ("#userPhoto") [0].submit ();
  23. return true;
  24. }
  25. } Else {
  26. Alert ("upload images in jpg,gif,bmp,jpeg,png format only");
  27. return false;
  28. }
  29. return true;
  30. }



3. Supplement:

The above about the verification of IE, suitable for the previous version of IE6, for IE7 version of the foreground is not good to judge the size of the picture, it is best to judge by the background:

(1) Submit the form first

(2) in the background to determine the size of the submission picture file, if it is greater than the maximum limit, do not do any processing, return the prompt, if within the maximum limit, then save and other processing

Using input to upload images, to determine the different browser and image type and size of the JS code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.