// 点击打开文件选择要传输的图片
chooseImage() {
let that = this;
uni.chooseImage({
count: 9, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
let tempFilePaths = res.tempFilePaths; // 返回选定照片的本地文件路径列表, tempFilePath可以作为img标签的src属性显示图片
console.log(tempFilePaths)
that.upload({
path: tempFilePaths
})
}
})
},
//上传图片
upload(data) { // 上传图片
var that = this,
i = data.i ? data.i : 0,
success = data.success ? data.success : 0,
fail = data.fail ? data.fail : 0;
uni.showToast({
icon: "loading",
title: "正在上传"
}),
uni.uploadFile({
//上传图片协议接口
url: 'https://dttll.dinoopnn.com/api/oss/upload',
filePath: data.path[i],
name: 'file',
header: {
"Content-Type": "multipart/form-data",
'Authorization': 'Bearer ' + uni.getStorageSync('token')
},
formData: {
type: "driver"
},
success(res) {
const code = res.statusCode.toString()
if (code.startsWith('2') || code === '304') {
success++;
let arr = JSON.parse(res.data);
let urlList = that.urlList.concat(arr.data);
that.urlList=urlList;
} else if (code == -1) {
uni.showModal({
title: '提示',
content: '登录状态过期,是否重新登录',
showCancel: false,
confirmText: '是',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
} else {
fail++;
uni.showModal({
title: '提示',
content: '上传失败',
showCancel: false
})
return;
}
},
fail(e) {
fail++;
uni.showModal({
title: '提示',
content: '上传失败',
showCancel: false
})
},
complete() {
uni.hideToast(); //隐藏Toast
console.log(i);
i++;
if (i == data.path.length) { //当图片传完时,停止调用
//this.save();//上传完后再执行的方法
console.log('执行完毕');
console.log('成功:' + success + " 失败:" + fail);
} else { //若图片还没有传完,则继续调用函数
console.log(i);
data.i = i;
data.success = success;
data.fail = fail;
that.upload(data);
}
}
})
},
小程序上传图片
最新推荐文章于 2024-03-29 11:17:14 发布