How to control the size of uploaded files with javascript _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

<form name=myform onsubmit= "return checkfilesize ()" >
<input Type=file name=photo><br/>
<input Type=submit value=submit></form>

<script language= "JavaScript" ><!--
function Checkfilesize ()
{
var s = document. Myform.photo.value;
if (s== "") return false;
var img = new Image ();
IMG.SRC = s;
Alert ("high =" + Img.height + "\ n width =" + img.width);
Alert ("fileSize =" + img.filesize + "byte");
return false;
}
--></script>

The FileSize attribute of JavaScript and IMG itself under IE is applied. In addition, IMG has several other attributes, such as Filecreateddate, Filemodifieddate, FileSize, fileupdateddate, filters, we can use these properties to get part of the picture file information, such as file size , we use the file form with the IMG tag to determine whether the size of the picture file is legal before uploading.
Copy Code code as follows:

<input type= "Text" size= "4" value= "ten" name= "Filesizelimit" id= "Filesizelimit"/> K
<input type= "File" Name= "File1" id= "file1" size= "" Onchange= "Changesrc" (This)/>

<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 large");
}
Else
{
Alert ("OK");
}
}
</script>

Copy Code code as follows:

function Checkfilesize ()
{
var filemaxsize = 100;//Limit uploaded file size, unit (k)
var s = document. RegForm.Img_1.value;
if (s== "") {alert ("No image,please select again!"); Document. RegForm.Img_1.focus (); return false;}
var img = new Image ();

IMG.SRC = s;

if (img.filesize>filemaxsize*1024) {alert ("The file size exceeds" +filemaxsize+ "k,please choose a smaller one!"); Document. RegForm.Img_1.focus (); return false;}

--></script>
<form action= "**.asp" method= "POST" onsubmit= "return Checkfilesize ()" enctype= "Multipart/form-data" Name= "RegForm ">
<input name= "Next" type= "Submit" class= "PBTTN" id= "Next" value= "Save" >

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.