google search

Monday, August 25, 2008

How to check on client side the file size in an Input File object?





<HTML>

<Head>

<Script Language=JavaScript>



function getStats(fName){



nullIMG.src = fName;

fullName = fName;

shortName = fullName.match(/[^\/\\]+$/);


splitName = fullName.split(".");

fileType = splitName[1];

fileType = fileType.toLowerCase();

if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')

{

checkIt = fileSize(fullName);

if (parseInt(checkIt) > 4000){alert('Max File Size is 4Kb');Form1.reset()}


else {

Form1.dispSize.value = checkIt;

Form1.dispName.value = shortName;

Form1.dispW.value = nullIMG.width;

Form1.dispH.value = nullIMG.height;

}

}

else {

alert("You must select an image file!");


Form1.reset();

}

}



divStr = "<Div Style='Position:Absolute;Top:-2000'><IMG Src=Null ID=nullIMG></Div>"

document.write(divStr)



</Script>


<Script Language=VBScript>



Function fileSize(fileSpec)

Dim isSize

Set fso = CreateObject("Scripting.FileSystemObject")

Set contentFile = fso.GetFile(fileSpec)

isSize = contentFile.Size

fileSize = isSize


Set contentFile = Nothing

Set fso = Nothing

End Function



</Script>

</Head>

<Body>

<Form name='Form1'>

Choose an image: <input type="file" name="isIMG" onChange="getStats(this.value)"><br>


File Name: <input name='dispName' size=20><br>

File Size: <input name='dispSize' size=10><br>

Width: <input name='dispW' size=5><br>

Height: <input name='dispH' size=5><br>

<input type=Reset><br>


<input type=submit value="Submit Form">

</Form>

</body>

</html>






No comments: