REBOL Programming/read-net

From Wikibooks, open books for an open world
< REBOL Programming
Jump to: navigation, search

USAGE:[edit]

READ-NET url /progress callback 

DESCRIPTION:[edit]

Read a file from the net (web). Update progress bar. Allow abort.

READ-NET is a function value.

ARGUMENTS[edit]

  • url -- (Type: url)

REFINEMENTS[edit]

  • /progress
    • callback -- Call func [total bytes] during transfer. Return true. (Type: any)

SOURCE CODE[edit]

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: to-integer any [port/locals/headers/content-length 8000] 
    buffer: make binary! size 
    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 size not callback size length? buffer data: true break] 
        not data
    ] 
    close port 
    if not data [buffer]
]