跳转到内容

Rebol 编程/read-net

来自维基教科书,开放的书籍,为开放的世界
READ-NET url /progress callback 

从网络(web)读取文件。更新进度条。允许中止。

READ-NET 是一个函数值。

  • url -- (类型:url)
  • /progress
    • callback -- 在传输期间调用 func [总字节数]。返回 true。 (类型:任何)

源代码

[编辑 | 编辑源代码]
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]
]
华夏公益教科书