From: vasudevram Date: 2007-07-11T22:17:33+09:00 Subject: Re: Get file size before download (noobish question) On Jul 8, 3:12 pm, Travis D Warlick Jr wrote: > Guest wrote: > > so the subject implies what i want to do, get the file size before > > downloading. i've seen posts about downloading the header, so if > > possible, could someone give an example of this code??? > > Net::HTTP.start('some.www.server', 80) do |http| > # Send a HEAD requesti > response = http.head('/index.html') > > # Get the Content-Length header from response['Your-Header-Here'] > len = response['Content-Length'].to_i > > } > > so if someone has any ideas as to getting filesize of a file (mp3 > > specifically) without a header... please help! > > Definitely not a noobish question. I've looked for a way to do this for > a while, but have found none. As far as I know, the only way to know > the Content-Length ahead of time is if the HTTP server gives it to you > in a HEAD request. > > -- > ************************************* > * Travis D Warlick, Jr > * Lead Developer > * Operis Systems, LLC > ************************************* Check the HTTP protocol spec (RFC - Google for it) to find out whether servers are required to send the Content-Length header - just to know. But that won't help you, of course, for those servers that don't do it anyway, even if required by the spec :-) This workaround is not tested, but you could try it: According the the HTTP spec, IIRC, the end of HTTP headers is indicated by two consecutive '\r\n' pairs, i.e. '\r\n\r\n'. So (if open-uri always pulls the whole file, as Robert Klemme says_, look in the Ruby docs for a read() method in some suitable Net class (maybe open-uri or Net:Http), to enable you to read the file by byres - write your code to look for the end of header indicator, and just stop reading after that. Then parse the headers you read, for the Content-Length field, to get the file size. Also try using HEAD instead of GET. If the server implements it correctly, it should only send the HTTP headers in this case. Vasudev Ram http://www.dancingbison.com http://jugad.livejournal.com http://sourceforge.net/projects/xtopdf