javascript| Control | upload
How do I control the size of uploaded files with javascript? --------This paper is to control the size of uploaded files using JavaScript and some properties such as filesize,fileupdateddate, such as the image in IE.
<form name=myform >
<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.
Restrictions: <input type= "Text" size= "4" value= "Name=" "Filesizelimit" id= "Filesizelimit"/> K
<input type= "File" Name= "File1" id= "File1" size= "-"/>
<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>
I use it in one of my Projects (Pendlehill)
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" enctype= "Multipart/form-data" name= "RegForm" >
<input name= "Next" type= "Submit" class= "PBTTN" id= "Next" value= "Save" >