uni.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
var tempFilePaths = res.tempFilePaths
uni.uploadFile({
url: url,
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success(res){
if(res.statusCode==200){
res.data = JSON.parse(res.data);
if(res.data.code==0){
console.log(res);
}
}
},
fail(res){
showToast('图片上传失败');
}
})
}
})
封装图片选取上传,能单张或多张
/**
* 图片选取上传
* type=0,只返会图片连接,其它则返回所有信息
* */
export const chooseImg = (count=1,type=0,formData={'user':'test'}) => {
const url = 'xxxxxxxxx';//上传路径
return new Promise((rel,rej) =>{
uni.chooseImage({
count, // 选取数量
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(r) {
uni.showLoading({
title:''
})
let arr = [];
r.tempFilePaths.map((item,index) =>{
//单张图片
uni.uploadFile({
url,
filePath: item,
name: 'file',
formData,
success(res){
uni.hideLoading()
if(res.statusCode==200){
res.data = JSON.parse(res.data);
if(res.data.code==1){
//只返回图片连接
if(type == 0){
arr.push(res.data.data.fullurl);
if(arr.length >= r.tempFilePaths.length){
rel(arr);
}
}else{
//返回图片所有数据
arr.push(res.data.data);
if(arr.length >= r.tempFilePaths.length){
rel(arr);
}
}
}
}
},
fail(){
uni.hideLoading()
}
})
})
}
})
})
}