JS Get upload file size

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax create document file file size files filesize function

About JS get upload file size processing principle is to use JS to read the current picture, and then create a new image and then the size of the picture read out, and then determine the current file picture size is not a specified size on OK.

<script language= "Web Effects" >
function showsize (Files)
{
var fso,f;
Fso=new ActiveXObject ("Scripting.FileSystemObject");
F=fso.getfile (files);
var filesize = f.size;
if ((filesize/1024) < 1024) {
Alert (filesize/1024+ "K");
}else if ((filesize/1024) > 1024) && ((filesize/(1024*1024)) < 1024)) {
Alert (filesize/(1024*1024) + "M");
}else{
Alert (filesize/(1024*1024*1024) + "G")
}
}
</script>

Method Two
: <input type= "Text" size= "4" value= "" Name= "Filesizelimit" id= "Filesizelimit"/> k
<input type = "File" name= "File1" id= "file1" size= "" Onchange= "Changesrc" (This)/>
<img src= "About:blank" Filechecker "alt=" test "  height="/>
   
<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 SCM ");
   }
    Else
    {
        alert ("OK");
   }
}
</script>

Method Three JS get upload file size
<script type= "Text/javascript" ><!--
function getfilesize1 (filename) {
var size = null;
var file = null;
if (filename) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
try{
File=fso.getfile (filename);
Size=file.size;
}
catch (e) {
Return ("Get file error.")
}
}
return (size);
}
-->
</script>

<div>
<input id= "upfile" type= "file" value= "Upload"/>
<button value= "Get Size" onclick= "var msg=getfilesize (document.getElementById (' Upfile '). value); alert (msg);"/ >
</div>

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.