/    Sign up×
Community /Pin to ProfileBookmark

File’s size and type Upload

I’m stuck with a javascript problem. Well, what I want to do is to upload
a gif or jpeg file from a form (file). Like any form validation, I want
to check the “Type” and the “Size” of the file. If the size is below
75000 bytes its ok, if its above, I want an alert error message.

I’ve tried something using ActiveX, but does not work

Can you please help me out of it?

to post a comment

1 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 11.2004 — Hi!

I hope you know, that this is IE only! Apart from that you will need a server side check for file type and size for security reasons.[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<body onReload=>
<script language="JavaScript" type="text/javascript">
function formCheck(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fileObj = fso.GetFile(document.myForm.whichFile.value);
nameOfFile = fileObj.Name;
typeOfFile = fileObj.Type;
sizeOfFile = fileObj.Size;
var sizeCheck=sizeOfFile;
sizeOfFile = sizeOfFile/1024;
sign = (sizeOfFile == (sizeOfFile = Math.abs(sizeOfFile)));
sizeOfFile = Math.floor(sizeOfFile*100+0.50000000001);
decimals = sizeOfFile%100;
sizeOfFile = Math.floor(sizeOfFile/100).toString();
decimals = "0" + decimals;
for (var i = 0; i < Math.floor((sizeOfFile.length-(1+i))/3); i++)
sizeOfFile = sizeOfFile.substring(0,sizeOfFile.length-(4*i+3))+','+
sizeOfFile= (((sign)?'':'-') + sizeOfFile + '.' + decimals) + ' kB';
if (sizeCheck<=75000&&(ext1=='gif'||ext1=='jpg'||ext1=='jpeg')){
alert('The file "'+nameOfFile+'" will be uploaded:nSize: '+sizeOfFile+'nType: '+typeOfFile);
return true;
if (sizeCheck>75000){
alert('The file "'+nameOfFile+'" is bigger than the allowed maximum of 75000 bytes ('+sizeOfFile+')');
return false;
else if (ext1!='gif'&&ext1!='jpg'&&ext1!='jpeg'){
alert('Allowed extensions are ".gif", ".jpg" and ".jpeg"!');
return false;
<form method="POST" name="myForm" enctype="multipart/form-data" onsubmit="return formCheck()">
<input type = "submit" value="upload">
<input type = file name="whichFile">
Cheers - Pit


Help @xorkeus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 10.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @ddiebold17,
tipped: article
amount: 1000 SATS,

tipper: @Mqlinka19,
tipped: live stream
amount: 4900 SATS,

tipper: @Mqlinka19,
tipped: article
amount: 10 SATS,