Skip to content

Commit

Permalink
补充uploadFile的timeout,fileName参数功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoguoweiLLHC authored and ZakaryCode committed Dec 14, 2020
1 parent 66ae9bd commit 0bcffb9
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/taro-h5/src/api/fileTransfer/uploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
XHR_STATS
} from './utils'

const createUploadTask = ({ url, filePath, formData, name, header, success, error }) => {
let timeout
const createUploadTask = ({ url, filePath, formData, name, header, timeout, fileName, success, error }) => {
let timeoutInter
let formKey
const apiName = 'uploadFile'
const xhr = new XMLHttpRequest()
Expand Down Expand Up @@ -42,7 +42,7 @@ const createUploadTask = ({ url, filePath, formData, name, header, success, erro

xhr.onload = () => {
const status = xhr.status
clearTimeout(timeout)
clearTimeout(timeoutInter)
success({
errMsg: `${apiName}:ok`,
statusCode: status,
Expand All @@ -51,22 +51,22 @@ const createUploadTask = ({ url, filePath, formData, name, header, success, erro
}

xhr.onabort = () => {
clearTimeout(timeout)
clearTimeout(timeoutInter)
error({
errMsg: `${apiName}:fail abort`
})
}

xhr.onerror = e => {
clearTimeout(timeout)
clearTimeout(timeoutInter)
error({
errMsg: `${apiName}:fail ${e.message}`
})
}

const send = () => {
xhr.send(form)
timeout = setTimeout(() => {
timeoutInter = setTimeout(() => {
xhr.onabort = null
xhr.onload = null
xhr.upload.onprogress = null
Expand All @@ -76,12 +76,12 @@ const createUploadTask = ({ url, filePath, formData, name, header, success, erro
error({
errMsg: `${apiName}:fail timeout`
})
}, NETWORK_TIMEOUT)
}, timeout || NETWORK_TIMEOUT)
}

convertObjectUrlToBlob(filePath)
.then(fileObj => {
form.append(name, fileObj, fileObj.name || `file-${Date.now()}`)
form.append(name, fileObj, fileName || fileObj.name || `file-${Date.now()}`)
send()
})
.catch(e => {
Expand All @@ -94,7 +94,7 @@ const createUploadTask = ({ url, filePath, formData, name, header, success, erro
* 中断任务
*/
const abort = () => {
clearTimeout(timeout)
clearTimeout(timeoutInter)
xhr.abort()
}

Expand Down Expand Up @@ -137,12 +137,14 @@ const createUploadTask = ({ url, filePath, formData, name, header, success, erro
* @param {string} object.name 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
* @param {Object} [object.header] HTTP 请求 Header,Header 中不能设置 Referer
* @param {Object} [object.formData] HTTP 请求中其他额外的 form data
* @param {number} [object.timeout] 超时时间,单位为毫秒
* @param {string} [object.fileName] 上传的文件名
* @param {function} [object.success] 接口调用成功的回调函数
* @param {function} [object.fail] 接口调用失败的回调函数
* @param {function} [object.complete] 接口调用结束的回调函数(调用成功、失败都会执行)
* @returns {UploadTask}
*/
const uploadFile = ({ url, filePath, name, header, formData, success, fail, complete }) => {
const uploadFile = ({ url, filePath, name, header, formData, timeout, fileName, success, fail, complete }) => {
let task
const promise = new Promise((resolve, reject) => {
task = createUploadTask({
Expand All @@ -151,6 +153,8 @@ const uploadFile = ({ url, filePath, name, header, formData, success, fail, comp
name,
filePath,
formData,
timeout,
fileName,
success: res => {
success && success(res)
complete && complete()
Expand Down

0 comments on commit 0bcffb9

Please sign in to comment.