How to determine the file size using JavaScript

Source: Internet
Author: User

For Web developers, file uploading is common and it is essential to determine the file size. Here we mainly introduce several common methods for JavaScript to determine the file size.

The first method is to apply ActiveX controls, for example:

JavaScript Code







This method can be implemented and easily thought by developers, but the only drawback is that there is a security prompt. Of course, change the file name. HTA will block the security prompt, but it is difficult to obtain it. Not recommended, not to mention

Next, let's talk about another method. There is an IMG tag in the HTML Tag that is not "deep" for general developers. let's first talk about its attributes: SRC, DYNSRC, start, ALT, controls, loop, loopdelay, hspace, vspace .... some common attributes will not be listed. Here we will talk about the "DYNSRC" attribute: DYNSRC can be used to insert various multimedia, the format can be WAV, Avi, AIFF, AU, MP3, Ra, ram, and so on. The URL is an audio or video file and its path, which can be a relative or absolute path.

Example:

In this way, the path of any type of file can be dynamically assigned based on DYNSRC, and the file size can be obtained in Javascript Based on The filesize attribute of the image object. Of course, the image object has several other attributes, such as filecreateddate, filemodifieddate, filesize, fileupdateddate, and filters.... The Code is as follows:

JavaScript code
<SCRIPT type = "text/JavaScript">
Function getfilesize (filepath)
{
VaR image = new image ();
Image. DYNSRC = filepath;
Alert (image. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value)">
</Body>
<SCRIPT type = "text/JavaScript">
Function getfilesize (filepath)
{
VaR image = new image ();
Image. DYNSRC = filepath;
Alert (image. filesize );
}
</SCRIPT>
<Body>
<Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value)">
</Body>

After testing, the file size is obtained, and no security prompt is displayed. It is recommended.

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.