Js detection file type and size

Source: Internet
Author: User
Function CheckFileType ()
{
Var objButton = document. getElementById ("buttonsubmit ");
Var objFileUpload = document. getElementById ('upload ');

Var objMSG = document. getElementById ('msg ');
Var FileName = new String (objFileUpload. value );
Var extension = new String (FileName. substring (FileName. lastIndexOf (".") + 1, FileName. length ));

Var image = new Image ();
Image. dynsrc = objFileUpload. value;
Var FileSize = Math. round (image. fileSize/1024*100)/100;
If (FileSize> 100)
{
ObjButton. disabled = true;
ObjMSG. innerHTML = "<div style = 'border: # CCCCCC dotted 1px; background-color: # FFFFCC; padding: 5px; margin: 10px auto; color: # FF0000; '> File too large. </div> ";
Return;
}

If (extension. toLowerCase () = "xls" | extension. toLowerCase () = "csv ")
{
ObjButton. disabled = false;
ObjMSG. innerHTML = "";
}
Else
{
ObjButton. disabled = true;
ObjMSG. innerHTML = "<div style = 'border: # CCCCCC dotted 1px; background-color: # FFFFCC; padding: 5px; margin: 10px auto; color: # FF0000; '> FileType error. </div> ";
}
}

______________________________________________________________
Function CheckFileType ()
{
Var objButton = document. getElementById ("buttonsubmit ");
Var objFileUpload = document. getElementById ('upload ');
Var FileSize = Math. round (getFileSize (objFileUpload. value)/1024*100)/100; // obtain the image file size
Var objMSG = document. getElementById ('msg ');
Var FileName = new String (objFileUpload. value );
Var extension = new String (FileName. substring (FileName. lastIndexOf (".") + 1, FileName. length ));

If (FileSize> 100)
{
ObjButton. disabled = true;
ObjMSG. innerHTML = "<div style = 'border: # CCCCCC dotted 1px; background-color: # FFFFCC; padding: 5px; margin: 10px auto; color: # FF0000; '> File too large. </div> ";
Return;
}

If (extension. toLowerCase () = "xls" | extension. toLowerCase () = "csv ")
{
ObjButton. disabled = false;
ObjMSG. innerHTML = "";
}
Else
{
ObjButton. disabled = true;
ObjMSG. innerHTML = "<div style = 'border: # CCCCCC dotted 1px; background-color: # FFFFCC; padding: 5px; margin: 10px auto; color: # FF0000; '> FileType error. </div> ";
}
}

Function getFileSize (filePath)
{
Var image = new Image ();
Image. dynsrc = filePath;

Return image. fileSize; // you can check the size of an image file.

}

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.