JavaScript input type=file Get file size and type restrictions

Source: Internet
Author: User


HTML code:

<input name= "txtname" type= "file" id= "pic" onchange= "LoadImage (This)"/>

JavaScript code:

Function loadimage (IMG)  {            var  filePath = img.value;             Var fileext = filepath.substring (Filepath.lastindexof ("."))                 .tolowercase () ;            if  (!CHECKFILEEXT (FILEEXT))  {                alert (" The file you uploaded is not a picture, please re-upload! ");                 img.value  =  "";                 return;            }             if  (Img.files && img.files[0])  {//                 alert (IMG);//                 alert (Img.files[0])                  alert (' The file size you selected '  +  (Img.files[0]. size / 1024). toFixed (0)  +  "KB");//                 var xx = img.files[0];//                 for  (VAR&NBSP;I&NBSP;IN&NBSP;XX)  {//                     alert (Xx[i])//                 }            } else {                 img.select ();                 var url =  Document.selection.createRange () .text;                 try {                     var fso = new activexobject (" Scripting.FileSystemObject ");                 } catch  (e)  {                     alert (' If you're using IE8 below   Please turn down the security level! ');                 }                 alert ("File Size:"  +  (FSO. GetFile (URL). size / 1024). toFixed (0)  +  "KB");             }        }          function checkfileext (EXT)  {             if  (!ext.match (/.jpg|. Gif|. Png|. bmp/i))  {                 return false;            }             return true;         }


This article is from the "focus area of it elite" blog, so be sure to keep this source http://msuccess.blog.51cto.com/5486886/1636377

JavaScript input type=file Get file size and type restrictions

Related Article

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.