Front-end limit types of uploaded files

Source: Internet
Author: User

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

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: 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.