determine file size from JS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

determine file size from JS

Post by Burrito »

I have a form with an input type=file.

I'd like to determine the file size prior to the submission of the form and do something with it in JS.

how to do...
for to drink..
or make smeary p--
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

im pretty sure its not possible unless you have the user input the filename manually but thats not a good idea
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the file field is readable. JS can create a loadable reference to the file through the DOM, however, this asks the browser to load the file. At this point the browser could easily run out of ram if the file is "large" ... There was a perl script posted/linked-to some time ago that allowed the progress bar to be fairly accurate (based on the submission headers) I think..
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

any ideas where I might such a post?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I found this and it works ok...probably only works in IE and in a controlled environment...luckly for me I have both and will ONLY have both :P

Code: Select all

function showSize(){
var objSize = new ActiveXObject(&quote;Scripting.FileSystemObject&quote;);

	var strFileName = objSize.getFile(document.MyForm.bob.value);
	var SizeOfFile = strFileName.size;
	alert(SizeOfFile + &quote; bytes&quote;);
	return false;	
}
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post by wyred »

Just in case you haven't heard of this PHP alternative.
The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.
http://www.php.net/manual/en/features.file-upload.php
Post Reply