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!

···

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

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:

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!

That is not possible!

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

···

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

Guest wrote:

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!

That is not possible!

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

···

On Jul 8, 3:12 pm, Travis D Warlick Jr <warli...@operissystems.com> 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
  *************************************

John Joyce wrote:

Guest wrote:

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!

That is not possible!

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.

···

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

John Joyce wrote:

Guest wrote:

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!

That is not possible!

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.

···

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

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

Taras Koval wrote:

John Joyce wrote:

Guest wrote:

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!

That is not possible!

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;
}

···

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

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 mailadmin@hexaware.com .
***************************************************************************************************************************

Taras Koval wrote:

Taras Koval wrote:

John Joyce wrote:

Guest wrote:

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!

That is not possible!

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;
}

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

···

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

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

···

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

Shai Rosenfeld wrote:

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

Example:

<html>
<head>
<script type="text/javascript">
function getFileSize() {
    var max_allowed_size = 15; // for example :slight_smile:
    var filesize = null;
    var input_file = document.getElementById("input_file").value;
    var browserName=navigator.appName;
    if (browserName=="Microsoft Internet Explorer"){
        var oas = new ActiveXObject("Scripting.FileSystemObject");
        var e = oas.getFile(input_file);
        filesize = e.size;
    }else{

  netscape.security.PrivilegeManager.enablePrivilege('*Universal**FileAccess*');
  var file = new java.io.File(filename);
  // Get the number of bytes in the file
  filesize = file.length();

    }
    if(filesize > max_allowed_size){
       return false;
    }
    return true;
}
</script>
</head>
<body>
<form onsubmit="getFileSize()">
<input type="file" value="Upload file" id="input_file">
</form>
</body>
</html>

A quick google finds this setting for lighttpd:

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

···

-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Shai Rosenfeld
Sent: Tuesday, July 10, 2007 2:16 AM
To: ruby-talk ML
Subject: Re: Get file size before download (noobish question)

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

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

Taras Koval wrote:

Shai Rosenfeld wrote:

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

Example:

<html>
<head>
<script type="text/javascript">
function getFileSize() {
   var max_allowed_size = 15; // for example :slight_smile:
   var filesize = null;
   var input_file = document.getElementById("input_file").value;
   var browserName=navigator.appName;
   if (browserName=="Microsoft Internet Explorer"){
       var oas = new ActiveXObject("Scripting.FileSystemObject");
       var e = oas.getFile(input_file);
       filesize = e.size;
   }else{
    netscape.security.PrivilegeManager.enablePrivilege('*Universal**FileAccess*');

    var file = new java.io.File(filename); // Get the number of bytes in the file
    filesize = file.length();

   }
   if(filesize > max_allowed_size){
      return false;
   }
   return true;
}
</script>
</head>
<body>
<form onsubmit="getFileSize()">
<input type="file" value="Upload file" id="input_file">
</form>
</body>
</html>

netscape.security.PrivilegeManager.enablePrivilege("Universal FileAccess");

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 )

···

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