JS to determine the size of the upload file

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax file file size file upload files function html http

Below provide three pages of special effects to judge upload file size Oh, these three methods are now limited file upload size is a better way, you can upload files when the user limit the size of the upload file to judge processing
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-type" content= "HTML; charset=gb2312 "/>
<title>js to judge upload file size </title>
</head>

<body>
Method One

<input type= "File" Name= "File1" onchange= "Showsize (this.value)" >
<script language= "Web Effects" >
<!--
function showsize (Files)
{
var fso,f;
Fso=new ActiveXObject ("Scripting.FileSystemObject");
F=fso.getfile (files);
if (f.size>1024*4) {
Alert (f.size+ "bytes");
}
}
-->
</script> method Two JS to determine the size of the upload file

<script type= "text/javascript"
    function Chksize (strfilename) {
         try
        {
          var maxsize = 100;    //definition of allowable file size, KB, please modify as required!
         var objstream = new ActiveXObject ("ADODB.stream");
         objStream.Type = 1;
         objStream.Open ();
         objStream.LoadFromFile (strFileName);
         if (Math.Round (objstream.size/1024) >maxsize)
              {alert ("Sorry!) The file you selected is "+math.round (objstream.size/1024,2) +" KB n exceeds the limit of the program "+maxsize+" KB! ");
             Document.form1.reset ();
             return false;
        }
         Else
             alert ("Can upload");
       }catch (e)
        {
            alert ("Not supported");
       }
   }
</script><
Form name= "Form1" action= "method=" POST "
<input type=" file "Name=" Upfile " Onchange= "Chksize (this.value)"/>//method three

<script>   
 
function getfilesize (filename) {   
     
if (document.all) {   
Window.oldonerror = window.onerror;        
Window.onerror = function (err) {   
if Err.indexof (' Utomation ')!=-1) {   
Alert (' No permissions to access files ');            
Return true;         
}         
else    
Return false;        
};   
    var fso = new ActiveXObject (' Scripting.FileSystemObject ' ;   
    var file = fso.getfile (filename);        
Window.onerror = window.oldonerror;   
    return file.size; }}   
</script>   
</head>    
  
<form name= "FormName" ><input type= "file" name= "filename" ><br>    
<input type= button "value=" View file byte size "onclick=" alert (GetFileSize (This.form.filename.value)) ><br>   
</form>
JavaScript to determine the size of the client file, JS to determine the size of the upload file, js get file length
</body
</html>
 

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.