JS upload file (image) Limited format and size of 3M

Source: Internet
Author: User

This article is saved as an. html file opened in a browser to test functionality

<form id= "Form1" Name= "Form1" method= "Post" action= "" enctype= "Multipart/form-data";
 <p> <input type= "hidden" name= "max_file_size" value= "100000"/></P>
 <p><input name= " UserFile "id=" UserFile "type=" file "onchange=" Check () "/></P>


</form>


< Script language= "javascript" type= "Text/javascript",
  function check ()
    {
var aa= document.getElementById ("UserFile"). Value.tolowercase (). Split ('. '); /with "." Separate the upload file string
   //var aa=document.form1.userfile.value.tolowercase (). Split ('. '); /with "." Separate upload file string
       
    if (document.form1.userfile.value== "")
    {
        alert (' Picture cannot be empty! ‘);
        return false;
   }
    Else
    {
    if (aa[aa.length-1]== ' gif ' | | aa[aa.length-1]== ' jpg ' | | aa[aa.length-1]== ' BMP '

|| aa[aa.length-1]== ' png ' | | aa[aa.length-1]== ' JPEG ')//Judging picture format
{
var imagsize = document.getElementById ("UserFile"). Files[0].size;
Alert ("Picture size:" +imagsize+ "B")
if (imagsize<1024*1024*3)
Alert ("The image size is within 3M, for:" +imagsize/(1024*1024) + "M");
return true;
}
Else
{
Alert (' Please select images formatted as *.jpg, *.gif, *.bmp, *.png, *.jpeg ');//jpg and JPEG format are just the same as the system Windows Jpg,mac OS recognition jpeg,

The difference between their own Baidu
return false;
}
}
}
</script>

JS upload file (image) Limited format and size of 3M

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.