提交 839f2fe7 编写于 作者: Q qiang

feat: H5 端 uni.uploadFile 支持 files 参数

上级 9244bd2d
......@@ -47,6 +47,7 @@ export function uploadFile ({
url,
filePath,
name,
files,
header,
formData
}, callbackId) {
......@@ -55,15 +56,23 @@ export function uploadFile ({
invokeCallbackHandler: invoke
} = UniServiceJSBridge
var uploadTask = new UploadTask(null, callbackId)
function upload (file) {
if (!Array.isArray(files) || !files.length) {
files = [{
name,
uri: filePath
}]
}
function upload (realFiles) {
var xhr = new XMLHttpRequest()
var form = new FormData()
var timer
Object.keys(formData).forEach(key => {
form.append(key, formData[key])
})
form.append(name, file, file.name || `file-${Date.now()}`)
Object.values(files).forEach(({ name }, index) => {
const file = realFiles[index]
form.append(name || 'file', file, file.name || `file-${Date.now()}`)
})
xhr.open('POST', url)
Object.keys(header).forEach(key => {
xhr.setRequestHeader(key, header[key])
......@@ -118,13 +127,16 @@ export function uploadFile ({
}
}
urlToFile(filePath).then(upload).catch(() => {
setTimeout(() => {
invoke(callbackId, {
errMsg: 'uploadFile:fail file error'
})
}, 0)
})
Promise
.all(files.map(({ name, uri }) => urlToFile(uri)))
.then(upload)
.catch(() => {
setTimeout(() => {
invoke(callbackId, {
errMsg: 'uploadFile:fail file error'
})
}, 0)
})
return uploadTask
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册