JS upload images before the restrictions include (jpg jpg gif and size width) and other _javascript skills

Source: Internet
Author: User
function
1. Limit extension: jpg only | | JPG and GIF
2. Limit picture size: K is the unit
3. Limit picture width High: PX as unit (either have, or none)
4. Limit damaged pictures (no preview pictures)
5. Limit the pictures that have changed the extension (such as forcing a dynamic GIF extension to JPG)
Use Restrictions
To add a onchange event to the Inputfile, make it available in an IMG tag when the file is selected, or use an error
Imglimit.js
Copy Code code as follows:

function limitimg () {
var Img=document.getelementbyid (arguments[0]);//Display the object of the picture
var maxsize=arguments[1];//
var allowgif=arguments[2]| | False
var maxwidth=arguments[3]| | 0;
var maxheight=arguments[4]| | 0;
var postfix=getpostfix (IMG.SRC);
var str= ". jpg";
if (allowgif) {str+= ". gif"}
if (Str.indexof (Postfix.tolowercase ()) ==-1) {
if (allowgif) {return "picture is not well-formed, only JPG or GIF images can be uploaded";} Else{return "Picture format is not correct, can upload jpg image";}
}else if (img.filesize>maxsize*1024) {
Return "picture size exceeds the limit, please limit in" +maxsize+ "K";
}else{
if (img.filesize==-1) {
Return "Picture format error, may be corrupted or change the extension cause, please select a picture again";
}else{
if (maxwidth>0) {
if (img.width>maxwidth) {
Return "The picture width exceeds the limit, please remain in" +maxwidth+ "pixel";
}else{
if (img.height>maxheight) {
Return "The picture height exceeds the limit, please remain in" +maxheight+ "pixel";
}else{
Return "";
}
}
}else{
Return "";
}
}
}
}
Get the file name extension based on the path
function Getpostfix (path) {
Return path.substring (Path.lastindexof ("."), path.length);
}

Page calls:
Copy Code code as follows:
<body>
<input type=" file "onchange=" document.getElementById (' Img1 '). Src=this.value; "/>

<input type=" button "onclick=" AA () "value=" Upload "/> ;
</body>
<mce:script type= "Text/javascript" ><!--
Function aa () {
If limitimg (' Img1 ', 10 0,false,1000,100 = = "") {
alert ("Picture upload succeeded");
}else{
Alert (limitimg (' img1 ', 100,false,1000,100));
}
}
//--></mce: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.