UploadTask.abort

解释:中断上传任务

# 方法参数

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例

    <view class="wrap">
        <view class="card-area">
            <button bind:tap="uploadFile" type="primary" hover-stop-propagation="true">点击向服务器上传图片</button>
            <button bind:tap="abortUploadFile" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">点击断掉前后端链接</button>
        </view>
    </view>
    
    
    Page({
        uploadFile() {
            this.setData({
                disabled: false
            });
            swan.chooseImage({
                success: res => {
                    const uploadTask = swan.uploadFile({
                        url: 'https://xxxx', // 仅为示例,并非真实的接口地址
                        filePath: res.tempFilePaths[0],
                        name: 'myfile',
                        header: {
                            'content-type': 'application/json'
                        },
                        formData: {
                            'user': 'swan'
                        },
                        success: res => {
                            swan.showModal({
                                title: 'title',
                                content: '上传成功'
                            });
                            console.log('uploadFile success', res);
                            this.setData({filePath});
                        },
                        fail: err => {
                            console.log('uploadFile fail', err);
                            swan.showToast({
                                title: '上传失败',
                                icon: 'none'
                            });
                        }
                    });
                    this.uploadTask = uploadTask;
                }
            });
        },
        abortUploadFile() {
            this.uploadTask.abort();
            swan.showModal({
                title: 'title',
                content: '已断掉'
            });
        }
    });