How to use javascript to control the size of uploaded files

Source: Internet
Author: User

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">

Related Article

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.