Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

hook while downloading from html port [read-net]

 [1/2] from: maximo::meteorstudios::com at: 15-Apr-2004 19:01


AND THE WINNER IS.........
> -----Original Message----- > From: Brett Handley > Have a look at /progress refinement on function Read-net.
;-) read-net... - EXACTLY - what I was looking for. another nifty function... cool thing is that its not a native, and we can source it, and then make our own (improved/specific version). Thanks for everyone who supplied a clue, they are all worthy avenues, and they all give usefull info which I will keep handy for other network-based problems. even remembering the existence of echo... --------------- PATCH --------------- here is a version of read-net which makes it more obvious if we know how many bytes we are expecting. The RT version puts an arbitrary 8000 bytes as the value... which will make any progress bar invalid. this version behaves EXACTLY the same internally (its still allocating an 8k buffer), but in the callback hook, it properly gives a none value as the total byte size of the remote file. This way your callback can adapt to only show downloaded bytes instead of a percentage (exactly like "save link as..." in explorer) . read-net: func [ {Read a file from the net (web). Update progress bar. Allow abort.} url [url!] /progress callback {Call func [total bytes] during transfer. Return true.} /local port buffer data size ][ vbug ['read-net url] if error? try [port: open/direct url] [return none] probe port/locals/headers/content-length size: either port/locals/headers/content-length [ to-integer port/locals/headers/content-length ][ none ] buffer: make binary! either integer? size [size][8000] set-modes port/sub-port [lines: false binary: true no-wait: true] until [ print type? size if not data: wait [60 port/sub-port] [data: true break] if data: copy port/sub-port [append buffer data] all [:callback not callback size length? buffer data: true break] not data ] close port if not data [buffer] ] -MAx

 [2/2] from: maximo:meteorstudios at: 15-Apr-2004 19:19


hi, wrt my last mail, the patch was ugly, containing prints and probes... here is a clean version with an example script to show the difference between loading a file for which size is not known and one that is: (as usual beware of wrapping lines...) ;-----------------------------patch------------------------------- read-net: func [ {Read a file from the net (web). Update progress bar. Allow abort.} url [url!] /progress callback {Call func [total bytes] during transfer. Return true.} /local port buffer data size ][ vbug ['read-net url] if error? try [port: open/direct url] [return none] size: either port/locals/headers/content-length [ to-integer port/locals/headers/content-length ][ none ] buffer: make binary! either integer? size [size][8000] set-modes port/sub-port [lines: false binary: true no-wait: true] until [ if not data: wait [60 port/sub-port] [data: true break] if data: copy port/sub-port [append buffer data] all [:callback not callback size length? buffer data: true break] not data ] close port if not data [buffer] ] ;--------------------------------Examples--------------------------------------- data: read-net/progress http://www.rebol.orgfunc [total bytes][print [bytes "/" total] true] data: read-net/progress http://ircuser.org/files/whackpak.wmv func [total bytes][print [bytes "/" total] true] ;---------- btw the second download is really cool... just do a write/binary on it. (its big though... 5,926,535 bytes) -MAx