IE + JS: detects the image file size before uploading October

Source: Internet
Author: User

Source: http://www.ugia.cn /? P = 73

I have to admire this person's clever thoughts. I just got a few simple words!

Before a file is uploaded, the file type is determined by the file name. However, it is difficult to check the file size unless it is local or a control is used. However, IMG has several additional attributes in IE, such as: filecreateddate, filemodifieddate,Filesize, Fileupdateddate, filters. We can use these attributes to obtain part of the image file information, such as the file size. We use the file form in combination with the IMG tag, you can determine whether the image file size is valid before uploading. The following is a demo:

Restrictions:K

Another small discovery is that the onload event in the IE animation is triggered at the beginning of each loop, so that we can use it to achieve the same effect as the setinterval () function in JS, such:

Loops: 0


The source code of these two examples is as follows:
First:

Restriction: <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>

Second:

loops: <span id = "loopsnum"> </span>

<SCRIPT type = "text/JavaScript">
VaR loops = 0;
Document. getelementbyid ("monkey"). onload = function ()
{
Document. getelementbyid ("loopsnum"). innertext = loops;
Loops ++;
}
</SCRIPT>

There are also some additional items in the manual:
<Input type = "hidden" name = "max_file_size" value = "30000"/>
Max_file_sizeThe hidden field (in bytes) must be prior to the input field, and its value is the maximum size of the received file. This is a suggestion for the browser. php will also check this item. This setting can be simply bypassed on the browser side, so do not expect this feature to block large files. In fact, the maximum value of uploaded files in PHP settings will not expire. However, it is best to add this project to the form, because it can avoid the trouble of File Upload failure only after the user spends the time waiting to upload a large file.

Note: The features discussed in this article are only valid in IE.

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.