JavaScript filesize
Get file size
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 | function filesize (url) { // Get file size // // version: 1109.2015 // discuss at: http://phpjs.org/functions/filesize // + original by: Enrique Gonzalez // + input by: Jani Hartikainen // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: T. Wild // % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain. // % note 1: Synchronous so may lock up browser, mainly here for study purposes. // * example 1: filesize('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm'); // * returns 1: '3' var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); if (!req) { throw new Error('XMLHttpRequest not supported'); } req.open('HEAD', url, false); req.send(null); if (!req.getResponseHeader) { try { throw new Error('No getResponseHeader!'); } catch (e) { return false; } } else if (!req.getResponseHeader('Content-Length')) { try { throw new Error('No Content-Length!'); } catch (e2) { return false; } } else { return req.getResponseHeader('Content-Length'); } } |
Examples
Running
1 | filesize('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm'); |
Should return
1 | '3' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have filesize goodness in JavaScript.
i have a problem for use this function on Internet Explorer, missing:
Object doesn't support this property or method
in this line:
if (!req.getResponseHeader)
broken and not continue.
¿what is the solution for this problem?
@ Arcanis & T. Wild: Thanks for your input guys. T.Wild, I've implemented your suggestion and am curious what people will say about this approach. If the responses are good we might implement it in other cases as well.
@Arcanis
While it may be stupid not to throw an error when the content length header isn't sent; when JavaScript throws an error the script halts, just try this:
[CODE="Javascript"]
throw new Error('error1');
alert("FOO"); //This is not called
[/CODE]
You don't want your entire script to halt simply because the server fails to send the correct headers which isn't a client side error.
However the XHR is different it is an error on the client side and does warrant an error, but even then I'd rather it return false rather than throw an error since PHP returns false on any error, not halt your script.
Maybe this would work?
[CODE type="Javascript"]
//This allows the error to be thrown while still returning false;
try{
throw new Error('error1');
}catch(e){
return false;
}
//In this case the error is still thrown AND the alert is called;
try{
throw new Error('error1');
}catch(e){};
alert("FOO");
[/CODE]
I know I may not be clear on what I'm saying but I hope you see what I'm getting at.
I think it's stupid to return false when there is no content-length, and to return an error when there is no XHR.
Both of them are errors, no ? I think you must send an error if the content-length is not specified.
While the whole method is a bit dubious (as pointed out by Onno Marsman), it might be a general improvement to check for window.XMLHttpRequest, rather than relying on error catching. To my understanding, catching exceptions (errors?) is slow.
@ Onno Marsman: Haha, that's ok Onno! appreciated. And about the compiler... Man I'm so busy :| Will try to dedicate more time to it.
@ Onno Marsman: I've made it return false if it doesn't. We've had this discussion before indeed. I know how you feel about this, and you know I agree but these functions serve a purpose of their own. We'll make sure they'll not be included by default with our future compiler. Thanks for your input Onno.
What if the "Content-Length" header does not exist?
Furthermore: it's really weird to use these types of functions. Making an Ajax request which executes the filesize function server-side would be many times better. But the same goes for something like file_get_contents and this has pointed out before. I really think that in the future these types of functions (along with functions like sleep, which also nobody should use) should not remain in the main download of php.js, but optionally available.


Prast
26 Aug '11