tags:

views:

73

answers:

5

given a path of a file like:
C:\file.jpg
how can i get the size of the file in javascript?

A: 

You can't get the file size of local files with javascript in a standard way using a web browser.

But if the file is accessible from a remote path, you might be able to send a HEAD request using Javascript, and read the Content-length header, depending on the webserver

baloo
i think the same, but how some smart web applications show progress bar while uploading?
Andrey
@Andrey using flash or a php module which can count bytes or similar.
baloo
@baloo flash is fine, but PHP?
Andrey
@Andrey there are module(s) for php to let you query the server to see how far the upload has progressed. I'm sorry but I don't remember the name. Also keep in mind that this could be stressful for the server as you keep querying it to see what the progress is. Flash has this counter on the clientside
baloo
+3  A: 

If it's not a local application powered by JavaScript, you can't. Web pages running javascript do not have access to the local filesystem for security reasons.

You can use a graceful degrading file uploader like SWFUpload if you want to show a progress bar.

Andy E
i think the same, but how some smart web applications show progress bar while uploading?
Andrey
@Andrey: Other web applications use Java or Flash. I updated my answer with a link to the popular [SWFUpload](http://www.swfupload.org/).
Andy E
@Andrey Other script tools such as Flash, ActiveX, etc can access the local file system. Alternatively, certain server-side techniques can be used to monitor file upload progress and feed that information back when javascript requests it
Mark Baker
i think i saw this tricks without flash
Andrey
check a link from another answer, scroll to end of page http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript
Andrey
@Andrey: that is not a cross-browser solution either. I don't think Firefox exposes the `fileSize` property, it's certainly not part of the standard. It might also only work for valid images and not other files.
Andy E
@Andy E's head - you convinced me :)
Andrey
@Andy E's head i am back. go to gmail and add attachment to letter. you see progress bar? there is no flash! please explain
Andrey
@Andrey: They probably use an application similar to [GWTUpload](http://code.google.com/p/gwtupload/), which queries the server for the file progress. This is a suitable method, but it obviously puts more strain on the server.
Andy E
@Andy E's head E's head progress is equal to total size / uploaded size, so did they manage to measure total size?
Andrey
@Andrey: The browser sends the total file size along with the multipart headers, so it's fairly easy to procure the total from those headers.
Andy E
@Andy E's head now i know what to do! get headers on server and drop transfer of body :) thank you for explanation
Andrey
A: 

You cannot as there is no file input/output in Javascript. See here for a similar question posted.

tommieb75
A: 

You cannot.

JavaScript cannot access files on the local computer for security reasons, even to check their size.

The only thing you can do is use JavaScript to submit the form with the file field to a server-side script, which can then measure its size and return it.

VoteyDisciple
+1  A: 

If you could access the file system of a user with javascript, image the bad that could happen.

However, you can use File System Object but this will work only in IE:

http://bytes.com/topic/javascript/answers/460516-check-file-size-javascript

Sarfraz
+1 there is awesome solution in last answer
Andrey