From: Konrad Meyer Date: 2007-12-03T07:29:09+09:00 Subject: Re: Trying to download first 10k --nextPart1386501.E3Tj4gvheY Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth David Beckwith: > Hi, I'm trying to download via http only the first, say, 10k of a file. > I'm using open-uri but even when I try to break out of the core loop > where it's reading the buffer, I can hear my hard-drive grinding away > downloading the entire 3MB. When I look at the file written to the disk > it shows only 10k, but judging by the length of time it takes to execute > the script and the grinding sound of my hard-drive, I think it's > downloading the entire file. >=20 > I read the other posts about a progress bar, but that's not really what > I'm trying to do..... or is it? >=20 > Is there a way to interrupt the download? As you can see I tried to > break out of the code blocks every chance I got. >=20 > Here is my code: >=20 > def download_first_n_kilobytes n, file_name > n =3D 1024 * n > uri =3D file_name > count =3D 0 > open(uri) do |fin| > open(convert_to_file_name(uri), 'w') do |fout| > while(buf =3D fin.read(n)) > count +=3D 1 > fout.write buf > if count > 0 > break > end # if > end # while > break > end # open > break > end # open > end # def >=20 > def convert_to_file_name uri > "mp3/" + File.basename(uri).to_s > end >=20 > uri =3D "http://www.sapht.com/music/tmp/sapht_-_alvaros_messenger.mp3" > download_first_n_kilobytes 10, uri >=20 >=20 > Thanks a lot for looking at this! You'll probably not want to be using open-uri but instead a custom HTTP que= ry=20 with a "Range: bytes=3D0-10239" header. HTH, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart1386501.E3Tj4gvheY Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHUzGrCHB0oCiR2cwRAvPFAJ9OOmJGpnttxIMz4K3oVjRS/JLu9gCgm/cg 29FXZ7BQhzaN8j4bZiDA00M= =XET8 -----END PGP SIGNATURE----- --nextPart1386501.E3Tj4gvheY--