// 点击单张图片上传 显示到页面
onUploads() {
var that = this;
uni.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['camera','album'],
success: (res) => {
const tempFilePaths = res.tempFilePaths;
console.log(tempFilePaths,'图片路径地址');
const serverUrl = '域名'
uni.uploadFile({
url: serverUrl + '/api/index/upload',
filePath: tempFilePaths,
name: 'image',
// header: {
// 'token': this.token,
// },
success: (resupload) => {
const result = JSON.parse(resupload.data);
console.log(result,'图片');
if (result.code == 1) {
this.fileList = this.fileList.concat(result.data);
if(this.fileList.length >= 9){
this.Uploadshow = false
} else {
this.Uploadshow = true
}
}
},
})
}
});
},
// 点击多张图片上传 显示到页面
async onUpload() {
if(this.fileList.length >= 5){
uni.showToast({
title:'最多上传五张',
icon:'none'
})
} else {
var that = this;
var newArrs = [];
var serverUrl = '域名'
var token = uni.getStorageSync('token');
let [err, chooseImageRes] = await uni.chooseImage({
count: 9
});
var chooeseArr = chooseImageRes.tempFilePaths;
var token = uni.getStorageSync('token');
var promiseimg = Promise.all(chooeseArr.map((chooeseArr, index) => {
return new Promise(function(resolve, reject) {
uni.uploadFile({
url: serverUrl + '/api/index/upload',
filePath: chooeseArr,
name: 'image',
header: {
'token': token,
// "Content-Type": "multipart/form-data",
},
success: (uploadFileRes) => {
resolve(JSON.parse(uploadFileRes.data));
},
fail: function(err) {
reject(err);
}
})
})
}));
promiseimg.then(results => {
for(var i in results) {
newArrs.push(results[i].data);
}
if(that.fileList.concat(newArrs).length){
var fileList = that.fileList.concat(newArrs);
that.fileList = fileList.slice(0,5);
}
}).catch(errs => {
console.log(errs);
});
}
},
官址: https://uniapp.dcloud.io/api/media/image?id=chooseimage
ios浏览器可以选择多张上传图片,android机 不行。。。。。。。。。。