Jump to content

Rebol Programming/read-net

From Wikibooks, open books for an open world

USAGE:

[edit | edit source]
READ-NET url /progress callback 

DESCRIPTION:

[edit | edit source]

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

READ-NET is a function value.

ARGUMENTS

[edit | edit source]
  • url -- (Type: url)

REFINEMENTS

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

SOURCE CODE

[edit | edit source]
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]
]