Get file size before download (noobish question)

G

Guest

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

Robert Klemme

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)

As far as I remember open-uri always pulls the whole thing. You would
have to use Net::HTTP to read and evaluate headers before you start
fetching the body. Maybe you can even issue a HEAD request but that's
just a wild guess. :)
so if someone has any ideas as to getting filesize of a file (mp3
specifically) without a header... please help!

The above only works if the server actually provides a value for
content-length in the header. AFAIK HTTP servers are not required to do
it so even if you look at headers before fetching the body you might not
get the length in every case.

Kind regards

robert
 
T

Travis D Warlick Jr

Guest said:
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
*************************************
 
V

vasudevram

Guest said:
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
 
T

Taras Koval

Guest said:
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!
 
T

Taras Koval

John said:
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.
 
T

Taras Koval

Taras said:
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;
}
 
T

Travis D Warlick Jr

Taras said:
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
*************************************
 
T

Travis D Warlick Jr

John said:
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
*************************************
 
S

Shai Rosenfeld

For IE:
... 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
 
T

Taras Koval

Shai said:
... 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 :)
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>
 
T

Taras Koval

Taras said:
Shai said:
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 :)
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");
 
S

Shai Rosenfeld

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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top