# UploadTask tap.uploadFile(Object object)

以 Promise 风格调用:不支持

相关文档: 网络使用说明、局域网通信

# 功能描述

将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-typemultipart/form-data

# 参数

# Object object

属性类型默认值必填说明
urlstring开发者服务器地址
filePathstring要上传文件资源的路径 (本地路径)
namestring文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
headerObjectHTTP 请求 Header,Header 中不能设置 Referer
formDataObjectHTTP 请求中其他额外的 form data
timeoutnumber超时时间,单位为毫秒
useHighPerformanceModebooleanfalse使用高性能模式,暂仅支持 Android,默认关闭。该模式下有更优的网络性能表现。
enableProfilebooleantrue是否开启 profile,默认开启。开启后可在接口回调的 res.profile 中查看性能调试信息。目前仅 iOS 端支持。
enableHttp2booleanfalse是否开启 http2
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

# object.success 回调函数

# 参数
# Object res
属性类型说明
datastring开发者服务器返回的数据
statusCodenumber开发者服务器返回的 HTTP 状态码

# 返回值

# UploadTask

一个可以监听上传进度进度变化的事件和取消上传的对象

# 示例代码

tap.chooseImage({
  success (res) {
    const tempFilePaths = res.tempFilePaths
    tap.uploadFile({
      url: 'https://example.xxx.com/upload', //仅为示例,非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        'user': 'test'
      },
      success (res){
        const data = res.data
        //do something
      }
    })
  }
})