...

View Full Version : Determine file size using Javascript



Pompiuses
09-19-2005, 04:14 PM
Is it possible to use Javascript to determine the size of a file before it's actually uploaded to the server using a multipart form?

mark87
09-19-2005, 04:29 PM
Nope, you can't because client side scripts cannot have access to files on the hard drive.

You could do it with ActiveX but highly recommend you don't do that.

Why not just check the filesize with a server side script?

Pompiuses
09-19-2005, 04:35 PM
Why not just check the filesize with a server side script?

Because them I have to upload the entire file to the server before I can check the size.

So if you have a slow connection it'll take several minutes to upload the file, and then it's a pain in the neck to have to wait several minutes to get the message back from the server that the file is too large.

martin_narg
09-19-2005, 07:21 PM
If you use an file upload component, it will tell you if the file size is too big without the whole file being fully uploaded.

There are many server-side file upload components on the market whcih enable easy saving and manipulation of files. Simply search on google for "file upload component" and whatever server-side language you are using (php/asp/etc).

m_n

TNO
09-20-2005, 10:13 AM
Client side solution:


function ShowFileSize(filespec)
{
var fso, f, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile(filespec);
s = f.Name + " uses " + f.size + " bytes.";
return(s);
}
This would obviously require some low security settings on the client or an .HTA file.

felgall
09-20-2005, 11:56 PM
That code will only work in browsers that support JScript (ie. IE) and don't have ActiveX disabled (ie. have ignored Microsoft's security suggestions).

The code will give an error if you try to run it as Javascript since Javascript doesn't recognise the ActiveX call - you should test if ActiveX is recognised first to determine whether the browser supports Javascript or JScript.

swarnaprava
11-02-2006, 08:48 AM
See if this code helps

<html>
<head>
<script language="JavaScript">
function A()
{
var oas = new ActiveXObject("Scripting.FileSystemObject");
var d = document.a.b.value;
var e = oas.getFile(d);
var f = e.size;
var mb=f/(1024);
alert(mb + "kilo bytes");
}
</script>
</head>
<body>
<form name="a">
<input type="file" name="b">
<input type="button" name="c" value="SIZE" onClick="A();">
</form>
</body>
</html>

swarnaprava
11-02-2006, 08:51 AM
<html>
<head>
<script language="JavaScript">
function A()
{
var oas = new ActiveXObject("Scripting.FileSystemObject");
var d = document.a.b.value;
var e = oas.getFile(d);
var f = e.size;
alert(f + " bytes");
}
</script>
</head>
<body>
<form name="a">
<input type="file" name="b">
<input type="button" name="c" value="SIZE" onClick="A();">
</form>
</body>
</html>

code for file size in bytes



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum