Js determines the size and size of the file to be uploaded in the file text field

Source: Internet
Author: User

Paste the following content to notepad, change the suffix to html to run ################################## ############### <input name = "txtImg" type = "file" id = "txtImg"/> <input name =" btnUploadImg "type =" button "id =" btnUploadImg "value =" Upload "/> <span class =" redcolor "> (optimal image size: the width cannot exceed 600 pixels, and the height cannot exceed 600 pixels.) </span> <script language = "javascript"> var AllowImgFileSize = 512; // The size of the image file that can be uploaded 0 is unrestricted unit: KB var AllowImgWidth = 600; // The width of the image that can be uploaded 0 is unrestricted unit: px (pixel) var AllowImg Height = 600; // The Height of the image to be uploaded 0 is unrestricted unit: px (pixel) function checkImg () {var txtImg = document. getElementById ("txtImg"); if (txtImg. value = "") {alert ("click the Browse button to select the JPG or GIF file you want to upload! "); TxtImg. focus (); return false;} var txtImg_url = txtImg. value. toLowerCase (); var txtImg_ext = txtImg_url.substring (txtImg_url.length-3, txtImg_url.length! = "Jpg" & txtImg_ext! = "Gif") {alert ("select a JPG or GIF file! "); TxtImg. select () document.exe cCommand ("Delete"); txtImg. focus (); return false;} var ErrMsgErrMsg = ""; // error message var img = new Image (); img. src = \ '# \' "/> if (img. width> AllowImgWidth) {ErrMsgErrMsg = "\ n the image width exceeds the limit. upload a file whose width is smaller than" + AllowImgWidth + "px. The current image width is" + img. width + "px"; www.2cto.com alert (ErrMsgErrMsg); return false;} if (img. height> AllowImgWidth) {ErrMsgErrMsg = "\ n the image height exceeds the limit. please upload the image height less than" + AllowImgHeight + "px The current image height is "+ img. height + "px"; alert (ErrMsgErrMsg); return false;} var size = formatNum (img. fileSize/, 2); if (size> AllowImgFileSize) {ErrMsgErrMsg = "\ n the image file size exceeds the limit. upload a file smaller than" + AllowImgFileSize + "KB, the current file size is "+ size +" KB "; alert (ErrMsgErrMsg); return false;} alert ('OK! '); Return true ;} ############ auxiliary functions ############### function formatNum (amt, pre) {pre = pre> 0 & pre <= 20? Pre: 2; amt = parseFloat (amt + ""). replace (/[^ \ d \. -]/g ,"")). toFixed (pre) + ""; var left = amt. split (". ") [0]. split (""). reverse (); var right = amt. split (". ") [1]; var t =" "; for (I = 0; I <left. length; I ++) {t + = left [I] + (I + 1) % 3 = 0 & (I + 1 )! = Left. length? "": "") ;}Return t. split (""). reverse (). join ("") + "." + right ;}</script>

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.