How to upload images through js verification _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to verify the upload of images by js. it is very simple and practical to restrict the type and size of the uploaded images, for more information about how to upload images, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:

 Js verification imageScript UpLoadFileCheck = function () {this. allowExt = ".jpg,.gif"; // the file type 0 that is allowed to be uploaded is unlimited. // A "," lowercase letter indicates this. allowImgFileSize = 0; // The size of the file to be uploaded 0 is unrestricted unit: KB this. allowImgWidth = 0; // the image width 0 that can be uploaded is unrestricted unit: px (pixel) this. allowImgHeight = 0; // The height of the image to be uploaded 0 is unrestricted unit: px (pixel) this. imgObj = new Image (); this. imgFileSize = 0; this. imgWidth = 0; this. imgHeight = 0; this. fileExt = ""; this. errMsg = ""; this. isImg = false; // global variable} UpL OadFileCheck. prototype. checkExt = function (obj) {this. errMsg = ""; this. imgObj. src = obj. value; // this. hasChecked = false; if (obj. value = "") {this. errMsg = "\ n select a file";} else {this. fileExt = obj. value. substr (obj. value. lastIndexOf (". ")). toLowerCase (); if (this. allowExt! = 0 & this. allowExt. indexOf (this. fileExt) =-1) // Determine whether the file type can be uploaded {this. errMsg = "\ n this file type cannot be uploaded. Upload a file of the "+ this. AllowExt +" type. the current file type is "+ this. FileExt ;}} if (this. ErrMsg! = "") {This. showMsg (this. errMsg, false); return false;} else return this. checkProperty (obj);} UpLoadFileCheck. prototype. checkProperty = function (obj) {if (this. imgObj. readyState! = "Complete") // {sleep (1000); // The image can be fully loaded in one second.} if (this. isImg = true) {this. imgWidth = this. imgObj. width; // Obtain the image width. this. imgHeight = this. imgObj. height; // Obtain the image height if (this. allowImgWidth! = 0 & this. AllowImgWidth
 
  
"); Msg = msg. replace (/\ n/gi ,"
  
  • "); */Alert (msg);} function sleep (num) {var tempDate = new Date (); var tempStr =" "; var theXmlHttp = new ActiveXObject (" Microsoft. XMLHTTP "); while (new Date ()-tempDate)
  • I hope this article will help you design javascript programs.

    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.