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

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

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