Get file size before download (noobish question)

Hi,

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???

also, for files without a header, i tried using :content_length_proc
within the open-uri class, but it would seem that the file is being
downloaded, then tested for size (from the difference in time for
running my code)

so if someone has any ideas as to getting filesize of a file (mp3
specifically) without a header… please help!

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

Guest wrote:

so if someone has any ideas as to getting filesize of a file (mp3
specifically) without a header… please help!

That is not possible!

On Jul 9, 2007, at 3:31 AM, Taras Koval wrote:

running my code)

so if someone has any ideas as to getting filesize of a file (mp3
specifically) without a header… please help!

That is not possible!

If you can ask the system the file is stored on, you can get its file
size.

Taras Koval wrote:

If you can ask the system the file is stored on, you can get its file
size.

Actually you can use JavaScript to get file size before uploading, I
know how to do that in IE and FF but there will be a browsers security
pop ups and user should confirm
that you script will be checking their file system.

For IE:

function getFileSize(file) {
var oas = new ActiveXObject(“Scripting.FileSystemObject”);
var e = oas.getFile(file);
var f = e.size;
return f;
}

The config/database.yml.sample file that I got thru svn client was
created on a Mac machine. I am currently using windows machine and I
tried copying the said file to config/database.yml using :

Copy config/database.yml.sample config/database.yml

In DOS prompt.

Then I tried creating the database using

rake create:db:all

and it is giving some funny syntax error on the line where repository
path is set. Even if I comment that line the syntax error just shifts to
the next line of execution.

And I am sure I have all the latest versions… of all relevant
softwares…

Has anyone ever experienced this… Any solutions?

Thanks,

Ujjwal


This e-mail communication and any attachments may be privileged and
confidential to Hexaware and are intended only for the
use of the recipients named above. If you are not the intended
recipient, please do not review, disclose, disseminate,
distribute or copy this e-mail and attachments. If you have received
this email in error, please delete the same alongwith
all attachments thereto and notify us immediately at
[email protected] .


John J. wrote:

also, for files without a header, i tried using :content_length_proc
If you can ask the system the file is stored on, you can get its file
size.

Actually you can use JavaScript to get file size before uploading, I
know how to do that in IE and FF but there will be a browsers security
pop ups and user should confirm
that you script will be checking their file system.

Taras Koval wrote:

possible, could someone give an example of this code???
That is not possible!

For IE:

function getFileSize(file) {
var oas = new ActiveXObject(“Scripting.FileSystemObject”);
var e = oas.getFile(file);
var f = e.size;
return f;
}

Note: that is only for uploading. I think he’s talking about
downloading.


  • Travis D Warlick, Jr
  • Lead Developer
  • Operis Systems, LLC

For IE:

function getFileSize(file) {
var oas = new ActiveXObject(“Scripting.FileSystemObject”);
var e = oas.getFile(file);
var f = e.size;
return f;
}

… as of uploading:

how do i use this function? i want to check out the file size before
allowing the user to upload a file or not (essentially, i should be able
to limit file size uploads from lighttpd [which is what i am running]
but i couldn’t seem to find any doc on how to do it) so i’m thinking of
sending an ajax request to see what the file size of the upload is, and
then pass the upload, if it is the right size.

the script above seems quite handy for this purpose (btw, this works
only for IE?)
but i don’t know what needs to go into the ‘file’ parameter … i tried
supplying a path to the file on my system, for a check
"getFileSize(’/home/shai/test.size’) " but that didn’t work … what
parameter am i supposed to supply to the function?

tia…

shai

John J. wrote:

also, for files without a header, i tried using :content_length_proc
If you can ask the system the file is stored on, you can get its file size.
It’s not possible to ask for a file size/content-length over HTTP.

Maybe FTP, Telnet, SSH, etc, but that’s assuming you have a login and
permissions to access the file.


  • Travis D Warlick, Jr
  • Lead Developer
  • Operis Systems, LLC

Taras Koval wrote:

only for IE?)
Example:
var oas = new ActiveXObject(“Scripting.FileSystemObject”);
if(filesize > max_allowed_size){

netscape.security.PrivilegeManager.enablePrivilege(“Universal
FileAccess”);

Shai R. wrote:

… as of uploading:
but i don’t know what needs to go into the ‘file’ parameter … i tried
supplying a path to the file on my system, for a check
"getFileSize(’/home/shai/test.size’) " but that didn’t work … what
parameter am i supposed to supply to the function?

tia…

shai

Example:

-----Original Message-----

var e = oas.getFile(file);

seem to find any doc on how to do it) so i’m thinking of
tia…

shai


Posted via http://www.ruby-forum.com/.

A quick google finds this setting for lighttpd:

http://trac.lighttpd.net/trac/wiki/server.max-request-sizeDetails

Rails has additional functionality to validate filesizes, if that is
what
you’re using as a framework. Other frameworks may offer similar
functionality.

HTH,

Felix

On Jul 8, 3:12 pm, Travis D Warlick Jr [email protected]
wrote:

len = response[‘Content-Length’].to_i


  • 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 :slight_smile:

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 K. 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://trac.lighttpd.net/trac/wiki/server.max-request-sizeDetails

HTH,

Felix

sweet. works great - when i upload a good file (small enough) everything
works, and when it is too large, the server doesn’t respond, and i get a
“The connection to the server was reset while the page was loading.”
message. that’s the good part… but how do i inform the user that he
was blocked because of the filesize?

i understand that there is a validates_filesize_of or something
similar-named, but i don’t have rmagick and i understood that was a
requirement …

so i’m thinking of using the script above, but this fails to deliver a
notice either … i’m working on firefox::ubntu (linux debian release)
… could this be the prob with the javascript?

bottom line - lighttpd limits filesize, but how do i notify the user in
the best manner? (this could all be done on the client-side, as lighttpd
blocks the file … but if the model validation works … that would go
too )