JS Get file Size method summary _javascript Skills

Source: Internet
Author: User

The example of this article summarizes the method of obtaining file size of JS. Share to everyone for your reference, specific as follows:

Method one, using ActiveX controls to implement:

<script type= "Text/javascript" >
function GetFileSize (filePath)
{
  var fso = new ActiveXObject (" Scripting.FileSystemObject ");
  Alert ("File size is:" +fso.) GetFile (FilePath). size);
</script>
<body>
<input type= "file" name= "file" size= "onchange=" GetFileSize (this.value) ;" >
</body>

This method can be used in IE, the deficiencies will have security prompts, the file name changed to. The HTA will block out the security prompts.

Method two, using the DYNSRC properties of IMG:

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= "onchange=" GetFileSize ( This.value) ">
</body>

This method can be used in IE6, and it cannot be used in Ie7,ie8,firefox,chrome.

Method III, using the FileSize of IMG:

Code:

<script language=javascript> var imgobj=new Image (); Create an Image object var allimgext= ". Jpg|. Jpeg|. Gif|. bmp|. Png| " All picture format type var fileobj,imgfilesize,imgwidth,imgheight,fileext,errmsg,filemsg,hascheked,isimg//global variable Picture related attributes// The following is the restriction variable var allowext= ". Jpg|. Gif|. doc|.  Txt| "///Allow uploaded file types?" for unrestricted each extension, add a "|" lowercase letter to denote the//var allowext=0 var allowimgfilesize=70;   Allow upload picture file size 0 is unrestricted: KB var allowimgwidth=500;   The width of the picture allowed to upload? For unrestricted units: px (pixel) var allowimgheight=500; 
The height of the picture allowed to upload? For unrestricted units: px (pixel) haschecked=false; 
 function Checkproperty (obj)//Detect Image property {fileobj=obj; 
   if (errmsg!= "")//detects if error information is returned for the correct image file and resets {showmsg (errmsg,false);   return false; 
   Returns} if (imgobj.readystate!= "complete")//If the image is not loaded completes for loop detection {settimeout ("Checkproperty (Fileobj)", 500); 
 return false; Imgfilesize=math.round (imgobj.filesize/1024*100)/100;//Gets the size of the picture file Imgwidth=imgobj.width//Get the width of the picture Imgheight=imgob  J.height; 
 Get the height of the picture filemsg= "\ n picture size:" +imgwidth+ "*" +imgheight+ "px"; Filemsg=filemsg+ "\ n picture file size:" +imgfilesize+ "Kb"; 
 filemsg=filemsg+ "\ n picture file name extension:" +fileext; if (allowimgwidth!=0&&allowimgwidth ' Else previewimg.inn 
    Erhtml= "Non-picture document"; 
    msglist.innerhtml=msg; 
 Haschecked=true; 
 } function Checkext (obj) {errmsg= ""; 
 Filemsg= ""; 
 Fileobj=obj; 
 Isimg=false; 
 Haschecked=false; 
 Previewimg.innerhtml= "Preview Area"; 
 if (obj.value== "") return false; 
 Msglist.innerhtml= "Document information processing ..."; 
 Document.all.uploadbutton.disabled=true; Fileext=obj.value.substr (Obj.value.lastIndexOf (".")). 
 toLowerCase (); if (Allowext!=0&&allowext.indexof (fileext+ "|") ==-1)//To determine whether the file type is allowed to upload {errmsg= "\ n" This file type is not allowed to upload. 
   Please upload "+allowext+" type of file, the current file type is "+fileext;" 
   ShowMsg (Errmsg,false); 
 return false; } if (Allimgext.indexof (fileext+ "|")! 
   =-1)//If the picture file, then carries on the picture information processing {isimg=true; 
   Imgobj.src=obj.value; 
   Checkproperty (obj); 
 return false; 
   else {filemsg= "\ n file extension:" +fileext; 
 ShowMsg (filemsg,true); 
 }} function Switchuptype (TF){if (tf) str= ' <input type= "file" Name= "File1" onchange= "Checkext (This)" style= "width:180px;" > ' Else str= ' <input type= ' text ' name= ' file1 ' onblur= ' Checkext (this) ' style= ' width:180px; 
   > ' document.all.file1.outerhtml=str; 
   Document.all.uploadbutton.disabled=true; 
 Msglist.innerhtml= ""; } </script> <form enctype= "Multipart/form-data" method= "POST" onsubmit= "return haschecked;" > <fieldset style= "width:372; height:60;padding:2px; " > <legend><font color= "#FF0000" > Photo source </font></legend> <input type= "Radio" name= "Radio1" "Checked onclick=" Switchuptype (true); > Local <input type= "Radio" name= "Radio1" onclick= "Switchuptype" (false); " > Remote: <input type= "file" Name= "File1" onchange= "Checkext (This)" style= "width:180px;" > <input type= "Submit" id= "Uploadbutton" value= "started uploading" disabled> <div style= "border:1 solid #808080; Backgrou Nd: #E0E0E0; width100%;height:20px;color: #606060;p adding:5px; " >; table border= "0" ><tr><td width= "id= previewimg" > Preview area </td><td id= "Msglist" valign= "Top"

 ></td></tr></table> </div> </fieldset> </form>

Can be used in ie,firefox,chrome, but only to determine the size of the picture file.

Attached: JS judge file format and size

Determines the photo size
function getphotosize (obj) {
  photoext=obj.value.substr (obj.value.lastIndexOf (".")). toLowerCase ()//Get file suffix name
  if (photoext!= '. jpg ') {
    alert ("Please upload a photo with suffix name jpg!");
    return false;
  }
  var fileSize = 0;
  var Isie =/msie/i.test (navigator.useragent) &&!window.opera;      
  if (Isie &&!obj.files) {     
     var filePath = obj.value;      
     var filesystem = new ActiveXObject ("Scripting.FileSystemObject");  
     var file = Filesystem.getfile (FilePath);        
     fileSize = file. Size;     
  } else { 
     fileSize = obj.files[0].size;   
  } 
  Filesize=math.round (filesize/1024*100)/100; The Unit is KB
  if (filesize>=10) {
    alert ("Photo maximum size is 10KB, please upload it again!");
    return false;
  }
}

JSP page:

Copy Code code as follows:
<input type= "File" id= "Imgfile name=" upload "style=" width:150px; "onchange=" getphotosize (this)/>

I hope this article will help you with JavaScript programming.

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.