views:

83

answers:

6

How about

<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>
A: 

It is not possible to get the size of a file using javascript in a way that will work on all browsers.

You could upload asynchronously the file using a flash uploader such as uploadify or swfupload and then, using the oncomplete callback, get the filesize... but that's really all.

marcgg
+3  A: 

Yes, it is impossible.

ActiveXObject will only work on Windows/InternetExplorer.

Pablo Santa Cruz
A: 

Using file system object is not recommended way to do it. Although it allows you to get the file size.

IE will issue a warning after it detects file system object. Other browsers possibly might do the same.

Sarfraz
+3  A: 

Javascript runs in a sandbox and is not allowed to get file object details from your local system. ActiveX is only IE specific and won't be able to do this in other browsers.

Sandbox implementation errors

Web browsers are capable of running JavaScript outside of the sandbox, with the privileges necessary to, for example, create or delete files. Of course, such privileges aren't meant to be granted to code from the web.

rahul
+5  A: 

You can't get the file size yourself but the browser will send the encoded file size to the server in the header before the actual file data.

So what you could do is start the upload and then send an AJAX request to the server asking for the size of the file. But there is another catch: The browser can encode the file for the transfer. This is often base64, so encoded size != file size but you can calculate the original file size: orig = encodedSize * 2 / 3

Aaron Digulla
A: 

This question is common among non-technical users and novice programmers, who mainly concentrate their minds on how things are done and not on why. Sure, you may find a way to do it in Internet Explorer using ActiveX or in Firefox using an extension, but you still need to stop and think if this is the right thing to do. You need to grant on a remote site access to the user's filesystem and the price to pay for this is too big, for the very little revenue of knowing on client side what the size of the file is.

So, without diving into specifications or asking questions you should know that this is impossible, because otherwise it will impose a great security risk on the HTML protocol and the browsers.

On rare occasions this is necessary, the right thing to do is to use a cross-browser plug-in, like a Java applet or Flash. These plug-ins will need to be signed, so that the users know to whom are granting access to their file systems.

kgiannakakis