Copy codeThe Code is as follows:
<Form name = Myform onsubmit = "return CheckFileSize ()">
<Input type = file name = photo> <br/>
<Input type = submit value = submit> </form>
<Script language = "JavaScript"> <! --
Function CheckFileSize ()
{
Var s = document. Myform. photo. value;
If (s = "") return false;
Var img = new Image ();
Img. src = s;
Alert ("height =" + img. height + "\ n width =" + img. width );
Alert ("fileSize =" + img. fileSize + "Byte ");
Return false;
}
// --> </SCRIPT>
The fileSize attribute of javascript and img in IE is applied. In addition, img has several other attributes, such as fileCreatedDate, fileModifiedDate, fileSize, fileUpdatedDate, and filters. We can use these attributes to obtain part of the image file information, such as the file size, by combining the file form with the img tag, we can determine whether the image file size is valid before uploading.
Copy codeThe Code is as follows:
<Input type = "text" size = "4" value = "10" name = "fileSizeLimit" id = "fileSizeLimit"/> K
<Input type = "file" name = "file1" id = "file1" size = "40" onchange = "changeSrc (this)"/>
<Script type = "text/javascript">
Var oFileChecker = document. getElementById ("fileChecker ");
Function changeSrc (filePicker)
{
OFileChecker. src = filePicker. value;
}
OFileChecker. onreadystatechange = function ()
{
If (oFileChecker. readyState = "complete ")
{
CheckSize ();
}
}
Function checkSize ()
{
Var limit = document. getElementById ("fileSizeLimit"). value * 1024;
If (oFileChecker. fileSize> limit)
{
Alert ("too large ");
}
Else
{
Alert ("OK ");
}
}
</Script>
Copy codeThe Code is as follows:
Function CheckFileSize ()
{
Var FileMaxSize = 100; // restrict the size of the uploaded file, in the unit of (k)
Var s = document. RegForm. Img_1.value;
// If (s = "") {alert ("No image, please select again! "); Document. RegForm. Img_1.focus (); return false ;}
Var img = new Image ();
Img. src = s;
If (img. fileSize> FileMaxSize * 1024) {alert ("The file size exceeds" + FileMaxSize + "K, please choose a smaller one! "); Document. RegForm. Img_1.focus (); return false ;}
// --> </SCRIPT>
<FORM action = "**. asp" method = "post" onSubmit = "return CheckFileSize ()" enctype = "multipart/form-data" name = "RegForm">
<Input name = "Next" type = "submit" class = "pBttn" id = "Next" value = "Save">