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