DownloadTask.onHeadersReceived

基础库 3.180.1 版本开始支持。

解释:监听下载的 HTTP Response Header 事件,一次连接只会触发一次,早于下载请求完成事件

# 方法参数

Function callback

# 返回参数说明

参数 类型 说明
header Object 开发者服务器返回的 HTTP Response Header

# 代码示例

    <view>
        <button type="primary" bindtap="onHeadersReceived">监听 HTTP Response Header</button>
    </view>
    
    Page({
        onHeadersReceived() {      
            let downloadTask = swan.downloadFile({
                url: 'https://smartprogram.baidu.com/docs/img/file-simple.pdf',
                header: {
                    'content-type': 'application/json'
                },
                success: () => {
                    console.log('downloadFile success');
                },
                fail: () => {
                    console.log('downloadFile fail');
                },
                complete: () => {
                    console.log('downloadFile complete');
                }
            });
            let cb = res => {
                swan.showModal({
                    title: 'onHeadersReceived',
                    content: JSON.stringify(res)
                });
            };
            downloadTask.onHeadersReceived(cb);
        }  
    });