Today at work encountered a demand, need to upload files when the type of upload files, such as uploading pictures can only upload image type of files. Now put your own code in the development of my blog, in case of future development again encountered. You can use it directly.
Here's a little demo. The code is as follows:
<! DOCTYPE html>varIsie =/msie/i.test (navigator.useragent) &&!Window.opera; functionFilechange (target,id) {varFileSize = 0; varFileTypes =[". jpg", ". png", ". rar", ". txt", ". zip", ". Doc", ". ppt", ". xls", ". pdf", ". docx", ". xlsx"]; varfilepath =Target.value; varFilemaxsize = 1024*2;//2M if(filepath) {varIsnext =false; varFileend = filepath.substring (Filepath.indexof ("."))); if(FileTypes && filetypes.length>0){ for(vari = 0; i<filetypes.length;i++){ if(filetypes[i]==fileend) {Isnext=true; Break; } } } if(!Isnext) {Alert ("Do not accept this file type!" "); Target.value=""; return false; } }Else{ return false; } if(Isie &&!)target.files) {varFilePath =Target.value; varFileSystem =NewActiveXObject ("Scripting.FileSystemObject"); if(!filesystem.fileexists (FilePath)) {Alert ("The attachment does not exist, please re-enter!" "); return false; } varFile =Filesystem.getfile (FilePath); FileSize=file. Size; } Else{fileSize= Target.files[0].size; } varSize = filesize/1024; if(size>filemaxsize) {Alert ("The attachment size cannot be greater than" +filemaxsize/1024+ "M! "); Target.value = ""; return false; } if(size<=0) {alert ("The attachment size cannot be 0m! "); Target.value=""; return false; } }</script></body>Front-end limit types of uploaded files