详细用法请查阅文档网页
小程序和网页通用上传资源库,用户上传图片、音频、文件
在小程序中:
- 引入包
const fileUpload = require('@lx-frontend/file-upload');
上传图片
wx.chooseImage({
'sizeType': ['compressed'],
success: async (res) => {
console.log('chooseImage res', res)
const [ localPath ] = res.tempFilePaths
const upRes = await fileUpload({
platform: 'mp',
uploadType: 'image',
configUrl: 'https://q.test.dos.lixinchuxing.cn/api/v1/upload/em_addition_repair_reason',
filePath: localPath,
header: { // 传递鉴权头2选1
Cookie: 'session=15417714-20a3-4458-94f9-fd83ef67cb6a',
'access-token': 'xxx'
}
})
console.log('upRes', upRes)
}
})
返回数据格式:
{
"code":0,
"data":[
{
"image":"https://test-images-cdn.lxusercontent.com/em_addition/repair_reason/g_zhongsheng/2021-02-20/25Lozpp9wHGgEUG3mBZELT.png",
"key":"",
"thumbnail_image":"https://test-images-cdn.lxusercontent.com/em_addition/repair_reason/g_zhongsheng/2021-02-20/25Lozpp9wHGgEUG3mBZELT.png?x-oss-process=image/resize,w_300/quality,q_80",
"upload_image":"https://test-images-cdn.lxusercontent.com/em_addition/repair_reason/g_zhongsheng/2021-02-20/25Lozpp9wHGgEUG3mBZELT.png"
}
],
"message":"ok"
}
上传音频
const record = wx.getRecorderManager()
record.start({
format: 'mp3'
})
record.onStop(async function (res) {
console.log('record res', res)
const recordRes = await fileUpload({
platform: 'mp',
uploadType: 'audio',
configUrl: 'https://q.test.dos.lixinchuxing.cn/api/v1/upload/em_addition_repair_reason_audio',
filePath: res.tempFilePath,
header: {
Cookie: 'session=15417714-20a3-4458-94f9-fd83ef67cb6a'
}
})
console.log('recordRes', recordRes)
})
setTimeout(() => {
record.stop()
}, 2000)
返回数据格式:
{
"code":0,
"data":[
{
"key":"",
"url":"https://test-images-cdn.lxusercontent.com/em_addition/repair_reason_audio/g_zhongsheng/2021-02-20/ffhxQtWr7Nfc7isziE28Tm.mp3"
}
],
"message":"ok"
}
参数 | 说明 | 类型 | 可选值 | 默认值 | 必填 |
---|---|---|---|---|---|
platform | 平台 | String | mp/web | Mp | 是 |
uploadType | 上传资源类型 | String | Image/audio | image | 是 |
configUrl | 每个新类型上传后台都会给一个 type ,追加到项目上传url后面,格式:https://项目域名/api/v1/upload/${type} ,举例: https://q.test.dos.lixinchuxing.cn/api/v1/upload/em_addition_repair_reason
|
String | - | - | 是 |
filePath | 小程序为选取资源的 tempFilePaths 字段,web 为form表单选择文件字段。 |
String | - | - | 是 |
header | 鉴权请求头,需要拿项目的 session/access-token 去指定url换成上传配置。举例:{ Cookie: 'session=15417714-20a3-4458-94f9-fd83ef67cb6a' } 或 { 'access-token': 'xxx' } | Object | - | {} | 是 |