The client verifies the size of the uploaded file.

Source: Internet
Author: User

Yesterday, Today, the two days are studying a question about how to restrict the size of uploaded files on the client. I have searched the internet for a long time. Firefox can directly obtain the size of the uploaded file for determination: fileupload. files [0]. fileSize;

Ie is different. First, you need to obtain the path of the local file and enable the Security Settings ActiveX function to obtain the file size and determine the file. The specific code is as follows:

Function checkFileSize (){
Var agent = window. navigator. userAgent;
Var fileupload = document. getElementById ("upfile ");
Var isIE7 = agent. indexOf ('msie 7.0 ')! =-1;
Var isIE8 = agent. indexOf ('msie 8.0 ')! =-1;
Var maxSize = 2000000;
Try {
// Firefox retrieval path
If (agent. indexOf ("Firefox")> = 1 ){
Var fileSize = fileupload. files [0]. fileSize;
} Else if (isIE7 | isIE8) {// obtain the file path from IE7 and IE8
Fileupload. select ();
Filepath = document. selection. createRange (). text;
Var aa = new ActiveXObject ("Scripting. FileSystemObject ");
Var fileSize = aa. GetFile (filepath). size;
} Else {// IE6 get the file path
Filepath = fileupload. value;
Var aa = new ActiveXObject ("Scripting. FileSystemObject ");
Var fileSize = aa. GetFile (filepath). size;
}
If (fileSize> maxSize ){
Alert ("the size of the uploaded file cannot exceed 2 MB! ");
Return false;
}
} Catch (e ){
Alert ("Please modify the ActiveX security settings of IE browser to enable ~! ");
Return false;
}
Return true;
}

In general, it is not ideal. if the customer is using the ActiveX function, what ActiveX function should be enabled? We look forward to a better and more concise method, so that users can use it more easily.

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.