This article describes the method of JavaScript to detect uploaded file size. Share it for your reference. The details are as follows:
The JS client code limits the size of the file uploaded by the user, but the client's verification is only auxiliary, and the server must do verification again.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head> <meta name="DEscription" contect="my code demo" /> <meta name="Author" contect="Michael@" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js check file size @ </title> </head> <body> <img dynsrc="" src="" style="display:none" /> <input type="file" name="file" size="40" /> <input type="button" name ="check" value="checkfilesize" onclick="checkfile()"/> </body> <script type="text/javascript"> var maxsize = 2*1024*1024;//2M var errMsg = "The uploaded attachment file cannot exceed 2M!!!"; var tipMsg = "Your browser does not support calculating the size of uploaded files. Make sure that the uploaded files should not exceed 2M. It is recommended to use IE, FireFox, and Chrome browsers."; var browserCfg = {}; var ua = ; if (("MSIE")>=1){ = true; }else if(("Firefox")>=1){ = true; }else if(("Chrome")>=1){ = true; } function checkfile(){ try{ var obj_file = ("fileuploade"); if(obj_file.value==""){ alert("Please select upload file first"); return; } var filesize = 0; if( || ){ filesize = obj_file.files[0].size; }else if(){ var obj_img = ('tempimg'); obj_img.dynsrc=obj_file.value; filesize = obj_img.fileSize; }else{ alert(tipMsg); return; } if(filesize==-1){ alert(tipMsg); return; }else if(filesize>maxsize){ alert(errMsg); return; }else{ alert("File size meets the requirements"); return; } }catch(e){ alert(e); } } </script> </html>
I hope this article will be helpful to everyone's JavaScript programming.