<uni-file-picker :value="filePathsList" fileMediatype="image" mode="grid" @select="select"
@delete="handleDelete" @progress="progress" @success="success" @fail="fail" :limit="1"
file-extname="png,jpg" />
async select(e) {
console.log('选择文件:', e)
// if()
await this.uploadImg(e.tempFilePaths, 1);
},
// 获取上传进度
progress(e) {
console.log('上传进度:', e)
},
// 上传成功
success(e) {
console.log('上传成功')
},
// 上传失败
fail(e) {
console.log('上传失败:', e)
},
handleDelete(err) { // 删除图片
const num = this.filePathsList.findIndex(v => v.url === err.tempFilePath);
this.filePathsList.splice(num, 1);
},
async uploadImg(tempFilePaths, token) {
// console.log(token)
if (!tempFilePaths.length) return;
const path = tempFilePaths.pop();
this.filePathsList.push({
url: path,
name: ""
})
const [err, {
data
}] = await uni.uploadFile({
url: config.uni_app_web_api_url '/upload',
filePath: path,
name: 'file',
header: {
// "Content-Type": "multipart/form-data",
}
});
// console.log("err", err)
// console.log("data", data)
let result = JSON.parse(data)
// console.log("status", )
if (result.status != 'ok') {
// upload fail
this.filePathsList.pop()
uni.showToast({
title: "上传失败",
icon: "none"
})
} else {
// upload success
console.log("data", result.data)
let img = result.data.split('/')
console.log(img)
this.filePathsList[this.filePathsList.length - 1].name = config.uni_app_web_api_url
"/public/img/" img[img.length - 1]
}
this.uploadImg(tempFilePaths, token);
},
2 个回复
2***@qq.com
遇到同样问题,请问怎么解决的呀
7***@qq.com
同样的问题,怎么解决阿