About JavaScript to determine the file size-

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

  1. <SCRIPT type = "text/JavaScript">
  2. Function getfilesize (filepath)
  3. {
  4. VaR FSO = new activexobject ("scripting. FileSystemObject ");
  5. Alert ("file size:" + FSO. GetFile (filepath). size );
  6. }
  7. </SCRIPT>
  8. <Body>
  9. <Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value);">
  10. </Body>
<SCRIPT type = "text/JavaScript"> function getfilesize (filepath) {var FSO = new activexobject ("scripting. fileSystemObject "); alert (" file size: "+ FSO. getFile (filepath ). size) ;}</SCRIPT> <body> <input type = "file" name = "file" size = "30" onchange = "getfilesize (this. value); "> </body>

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 we will talk about another method. There is an IMG tag in HTML tags that is not "deep" for general developers. let's first talk about its attributes:SRC, DYNSRC, start, ALT, controls, loop, loopdelay, heatace, 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:Filecreateddate, filemodifieddate, filesize, fileupdateddate, filters...The Code is as follows:

JavaScript code
  1. <SCRIPT type = "text/JavaScript">
  2. Function getfilesize (filepath)
  3. {
  4. VaR image = new image ();
  5. Image. DYNSRC = filepath;
  6. Alert (image. filesize );
  7. }
  8. </SCRIPT>
  9. <Body>
  10. <Input type = "file" name = "file" size = "30" onchange = "getfilesize (this. Value)">
  11. </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, no security prompt is displayed for the file size,Recommendation, not to mention

From: http://kokobox.javaeye.com/blog/227071

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.