diff --git a/packages/taro-alipay/src/native-api.js b/packages/taro-alipay/src/native-api.js index 7fd2bfdd7da4..1c1f8210f561 100644 --- a/packages/taro-alipay/src/native-api.js +++ b/packages/taro-alipay/src/native-api.js @@ -275,12 +275,16 @@ function processApis (taro) { }) if (newKey === 'uploadFile' || newKey === 'downloadFile') { p.progress = cb => { - task.onProgressUpdate(cb) + if (task) { + task.onProgressUpdate(cb) + } return p } p.abort = cb => { cb && cb() - task.abort() + if (task) { + task.abort() + } return p } } diff --git a/packages/taro-h5/types/index.d.ts b/packages/taro-h5/types/index.d.ts index 692b52cb4f9f..9d1b6decd4a9 100644 --- a/packages/taro-h5/types/index.d.ts +++ b/packages/taro-h5/types/index.d.ts @@ -222,6 +222,31 @@ declare namespace TaroH5 { */ statusCode: number } + /** + * 上传进度 + */ + type UploadTaskProgress = { + progress: number + totalBytesSent: number + totalBytesExpectedToSend: number + } + /** + * 上传进度回调 + */ + type UploadTaskProgressCallback = (res: UploadTaskProgress) => any + /** + * 上传任务 + */ + type UploadTask = Promise & { + /** + * 上传进度回调 + */ + progress: (UploadTaskProgressCallback) => void + /** + * 终止上传任务 + */ + abort: () => void + } type Param = { /** * 开发者服务器 url @@ -304,7 +329,7 @@ declare namespace TaroH5 { * ``` * @see https://developers.weixin.qq.com/miniprogram/dev/api/network-file.html#wxuploadfileobject */ - function uploadFile(OBJECT: uploadFile.Param): Promise + function uploadFile(OBJECT: uploadFile.Param): uploadFile.UploadTask namespace downloadFile { type Promised = { diff --git a/packages/taro-swan/src/native-api.js b/packages/taro-swan/src/native-api.js index e6e5716db2ce..a70f0b26b8f5 100644 --- a/packages/taro-swan/src/native-api.js +++ b/packages/taro-swan/src/native-api.js @@ -111,12 +111,16 @@ function processApis (taro) { }) if (key === 'uploadFile' || key === 'downloadFile') { p.progress = cb => { - task.onProgressUpdate(cb) + if (task) { + task.onProgressUpdate(cb) + } return p } p.abort = cb => { cb && cb() - task.abort() + if (task) { + task.abort() + } return p } } diff --git a/packages/taro-weapp/src/native-api.js b/packages/taro-weapp/src/native-api.js index 5e05e75dc33d..21bf7c5c1f57 100644 --- a/packages/taro-weapp/src/native-api.js +++ b/packages/taro-weapp/src/native-api.js @@ -138,12 +138,16 @@ function processApis (taro) { }) if (key === 'uploadFile' || key === 'downloadFile') { p.progress = cb => { - task.onProgressUpdate(cb) + if (task) { + task.onProgressUpdate(cb) + } return p } p.abort = cb => { cb && cb() - task.abort() + if (task) { + task.abort() + } return p } } diff --git a/packages/taro/types/index.d.ts b/packages/taro/types/index.d.ts index 725afc948670..bec65d92e823 100644 --- a/packages/taro/types/index.d.ts +++ b/packages/taro/types/index.d.ts @@ -653,6 +653,31 @@ declare namespace Taro { */ statusCode: number } + /** + * 上传进度 + */ + type UploadTaskProgress = { + progress: number + totalBytesSent: number + totalBytesExpectedToSend: number + } + /** + * 上传进度回调 + */ + type UploadTaskProgressCallback = (res: UploadTaskProgress) => any + /** + * 上传任务 + */ + type UploadTask = Promise & { + /** + * 上传进度回调 + */ + progress: (UploadTaskProgressCallback) => void + /** + * 终止上传任务 + */ + abort: () => void + } type Param = { /** * 开发者服务器 url @@ -735,7 +760,7 @@ declare namespace Taro { * ``` * @see https://developers.weixin.qq.com/miniprogram/dev/api/network-file.html#wxuploadfileobject */ - function uploadFile(OBJECT: uploadFile.Param): Promise + function uploadFile(OBJECT: uploadFile.Param): uploadFile.UploadTask namespace downloadFile { type Promised = {