title: Taro.uploadFile(option)
sidebar_label: uploadFile
将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data。使用前请注意阅读相关说明。
支持情况:
.default})
类型
(option: Option) => UploadTaskPromise
参数
| 参数 | 类型 |
|---|---|
| option | Option |
Option
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| url | string |
是 | 开发者服务器地址 |
| filePath | string |
是 | 要上传文件资源的路径 |
| name | string |
是 | 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容 |
| header | TaroGeneral.IAnyObject |
否 | HTTP 请求 Header,Header 中不能设置 Referer |
| formData | TaroGeneral.IAnyObject |
否 | HTTP 请求中其他额外的 form data |
| timeout | number |
否 | 超时时间,单位为毫秒 |
| fileName | string |
否 | 上传的文件名 API 支持度: h5 |
| complete | (res: TaroGeneral.CallbackResult) => void |
否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| fail | (res: TaroGeneral.CallbackResult) => void |
否 | 接口调用失败的回调函数 |
| success | (result: SuccessCallbackResult) => void |
否 | 接口调用成功的回调函数 |
SuccessCallbackResult
| 参数 | 类型 | 说明 |
|---|---|---|
| data | string |
开发者服务器返回的数据 |
| statusCode | number |
开发者服务器返回的 HTTP 状态码 |
| errMsg | string |
调用结果 |
示例代码
示例 1
Taro.chooseImage({success (res) {const tempFilePaths = res.tempFilePathsTaro.uploadFile({url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址filePath: tempFilePaths[0],name: 'file',formData: {'user': 'test'},success (res){const data = res.data//do something}})}})
示例 2
const uploadTask = Taro.uploadFile({url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址filePath: tempFilePaths[0],name: 'file',formData:{'user': 'test'},success: function (res){var data = res.data//do something}})uploadTask.progress((res) => {console.log('上传进度', res.progress)console.log('已经上传的数据长度', res.totalBytesSent)console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)})uploadTask.abort() // 取消上传任务
