[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Get file size before download (noobish question

guest

7/8/2007 4:34:00 AM

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

13 Answers

Robert Klemme

7/8/2007 7:00:00 AM

0

On 08.07.2007 06:34, 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???
>
> 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

Travis D Warlick Jr

7/8/2007 10:12:00 AM

0

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

vasudevram

7/8/2007 4:03:00 PM

0

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

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.dancin...
http://jugad.livej...
http://sourceforge.net/proje...


Taras Koval

7/9/2007 8:31:00 AM

0

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!

John Joyce

7/9/2007 1:32:00 PM

0


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!
>
If you can ask the system the file is stored on, you can get its file
size.

Taras Koval

7/9/2007 3:17:00 PM

0

John Joyce wrote:
>
> 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!
>>
> 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

7/9/2007 3:34:00 PM

0

Taras Koval wrote:
> John Joyce wrote:
>>
>> 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!
>>>
>> 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;
}


Travis D Warlick Jr

7/9/2007 9:47:00 PM

0

Taras Koval wrote:
> Taras Koval wrote:
>> John Joyce wrote:
>>>
>>> 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!
>>>>
>>> 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.

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

Travis D Warlick Jr

7/9/2007 9:51:00 PM

0

John Joyce wrote:
>
> 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!
>>
> 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
*************************************

Shai Rosenfeld

7/10/2007 9:16:00 AM

0

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