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.