Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check file size 1

Status
Not open for further replies.

magmo

Programmer
May 26, 2004
291
SE
Hi

I wonder if there is a way to check a file size before the file is uploaded to the server.


Regards
 
Try this if you can make out for yourself...
[tt]
function doit(fspec) {
var fsize;
if (window.ActiveXObject) {
fsize=(new ActiveXObject("scripting.filesystemobject")).getFile(fspec).size;
} else {
fsize=(new java.io.File(fspec)).length();
}
alert(fsize+" bytes");
}[/tt]
with calling config like this or expansion based on it.
[tt]
<input type="file" onchange="doit(this)" />
[/tt]
 
I don't think you can. the FILE input element is secured in most browser so you cannot do much with it - for obvious security reasons.

If you are looking to set a maximum file size then maybe this link will help )see the bottom):

I've not tested it, and am not convinced it is very compatible (probably just an IE feature), so you'll need to play around with it.

Probably better doing this server side.



A smile is worth a thousand kind words. So smile, it's easy! :)
 
If we recite web-pseudo-wisdoms thousands times without qualifying it, they risk to become truth and we all risk to all become philosophers.
 
tsuji,

I clearly stated that I don't think that you can. But as your code doesn't work (in FF or IE), I'm yet to be convinced otherwise - but would be very happy to see a working solution that was cross-browser compatible.

Besides, accessing the INPUT element with type=FILE and its related file IS restricted in several ways due to security - which is what I noted above - hardly a 'web-pseudo-wisdom' ...

seriously tsuji, chill-out.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
damber,

you need to chill out. tsuji has been an asset in the js forum for years. clearly, his code will not work in firefox, as active-x is IE only. his code will work with one minor change:

Code:
<input type="file" onchange="doit(this[red].value[/red])" />

the OP asked a question in the javascript forum. while telling them it can't work is probably the safest answer, tsuji has provided a javascript solution (with a minor flaw).

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Thanks cLFlaVA. That's what I meant. I do my posting by typing in real-time. My slip. It is this.value.
 
Upon reading your full post Cory, I need to add that I am too ambitious to make it work on moz as well. That's how it is constructed. I suppose jvm installed and some moz security twist that most moz fans seem to ignore to my surprise. But I won't insist. Each one makes out what one can.
 
Cory,

The code tsuji provided theoretically caters for both IE and non IE and doesn't work in either, with the object or the value passed to it.

Don't get me wrong, I'm not having a go at tsuji (even after his testing second post) - just correcting that somewhat accusing post and confirming what I meant. And I would be very interested to see a working solution.

Apologies if my last post came across in any way as stressed, it's been a long day.

Still... it doesn't work, and supposing that the vast majority of users (who are non-technical) will have just the right settings/config and software installed is presumptious to say the least.

But post back with any updates/corrections/ideas or even examples on the net that work - although I've never needed this particular feature, it would be nice to know.

And, guys... chill-out ;-) working together is much easier.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
clearly, the *best* solution to this issue (for cross-browser compatability, and to ensure that javascript haters still have this work for them) would be to use the server-side code (php, asp, cfm, what-have-you).

[&raquo;] (php)
[&raquo;] (asp.net)


*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
I'd agree, but it would be nice for this to work for those that have javascript enabled - as an added pre-check.

Maybe it is just my testing, but I cannot get the code tsuji posted to work in either FF (1.5) or IE(6) - I changed the parameter to getFile to the value rather than the object when I first tested (which is the right param as noted above) but it didn't work - just security errors.

I know this should work when run as a normal script (outside of the browser context) but when in the secure context of the browser - is it likely to work ? certainly not the full functionality of the filesystemobject as that would allow any website to compromise any visitors computer.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
probably your security settings. when i copied/pasted his code and tried it in IE, i got a warning about activex which i accepted. the application then prompted me with the size of the file i chose.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
It was my security settings in IE, tut tut. [blush]

Still cannot get it to work in FF 1.5 (java enabled) maybe it's due to it being a beta - what version are you running? But am just trawling through the huge about:config settings list to see if I can identify if any tweaks could be preventing it - if it works for you it will probably work in other cases - with a few tweaks to configuration.

So, as it does work it's an option, though not a reliable one. Star for tsuji for the code.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top