uni.$on('uAvatarCropper', uri => {
const { config, auth } = getApp().globalData
const url = `${config.api}/test/file?access_token=${auth.access_token}`
const files = [{ uri }]
console.log({ url, files })
uni.showLoading({ title: '上传中' })
uni.uploadFile({
url,
files
}).then(([ err, res ]) => {
uni.hideLoading()
if (err) {
uni.showModal({
title: '上传失败',
content: err,
showCancel: false
})
} else {
uni.showToast({ title: '上传成功' })
this.form.avatar = uri
getApp().globalData.user.avatar = uri
}
})
})
}
- 发布:2020-09-16 20:20
- 更新:2020-09-17 07:32
- 阅读:2509
产品分类: uniapp/小程序/微信
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 10
HBuilderX类型: Alpha
HBuilderX版本号: 2.8.11
第三方开发者工具版本号: 1.03
基础库版本号: 2.12.2
项目创建方式: HBuilderX
示例代码:
操作步骤:
2020-09-16 20:11:12,632 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,635 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272426.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\f663b583-4221-439d-96e8-6359500ac215.jpeg'
}
]
2020-09-16 20:11:12,636 DEBUG 2388 [middleware.request] POST /test/file 1ms 200 127.0.0.1
2020-09-16 20:11:12,637 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272429.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\32981a71-0233-416f-876f-459d98615f55.jpeg'
}
]
2020-09-16 20:11:12,639 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,641 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272432.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\c2410d85-5a46-45e2-8e49-076744a65ddb.jpeg'
}
]
2020-09-16 20:11:12,643 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,644 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272444.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\3e096477-b616-40c8-8d50-ed359f33d845.jpeg'
}
]
2020-09-16 20:11:12,632 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,635 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272426.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\f663b583-4221-439d-96e8-6359500ac215.jpeg'
}
]
2020-09-16 20:11:12,636 DEBUG 2388 [middleware.request] POST /test/file 1ms 200 127.0.0.1
2020-09-16 20:11:12,637 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272429.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\32981a71-0233-416f-876f-459d98615f55.jpeg'
}
]
2020-09-16 20:11:12,639 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,641 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272432.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\c2410d85-5a46-45e2-8e49-076744a65ddb.jpeg'
}
]
2020-09-16 20:11:12,643 DEBUG 2388 [middleware.request] POST /test/file 2ms 200 127.0.0.1
2020-09-16 20:11:12,644 DEBUG 2388 [middleware.development] NODE_ENV = development
[
{
field: 'file',
filename: '1600258272444.jpeg',
encoding: '7bit',
mime: 'image/jpeg',
fieldname: 'file',
transferEncoding: '7bit',
mimeType: 'image/jpeg',
filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\lpcheer-api\\2020\\09\\16\\20\\3e096477-b616-40c8-8d50-ed359f33d845.jpeg'
}
]
预期结果:
请求一次并返回
请求一次并返回
实际结果:
H5端请求多次返回,微信小程序端不发任何请求
H5端请求多次返回,微信小程序端不发任何请求
bug描述:
上传文件,小程序端一次请求都不发,H5端发一堆请求
z***@gmail.com (作者) - github.com/axolo
初步解决方案
- 小程序端不支持files:使用filePath而非files,多文件上传使用Promise.all
- 小程序端使用querystring.access_token引起Provisional headers are shown:改用header.Authorization
经鉴定,非uniapp自身bug。