代码如下,备忘:
/* 上传图片 开始 */
// 删除图片
deletePic(e) {
// console.log(e);
this[`picList${e.name}`].splice(e.index, 1)
this.news.morepics.splice(e.index, 1)
},
// 新增图片
async afterRead(event) {
let me = this;
// console.log(event)
// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
let lists = [].concat(event.file)
let picListLen = this[`picList${event.name}`].length
lists.map((item) => {
this[`picList${event.name}`].push({
...item,
status: 'uploading',
message: '上传中'
})
})
for (let i = 0; i < lists.length; i++) {
const result = await this.uploadFilePromise(lists[i].url)
let item = this[`picList${event.name}`][picListLen]
this[`picList${event.name}`].splice(picListLen, 1, Object.assign(item, {
status: 'success',
message: '',
url: result
}))
picListLen++
me.news.morepics.push(result)
// console.log(result)
}
console.log(me.news.morepics)
},
// 上传图片
uploadFilePromise(url) {
let me = this, apiUrl = this.apiUrl, token = uni.getStorageSync('token') || '';
return new Promise((resolve, reject) => {
let a = uni.uploadFile({
url:apiUrl + '/api.php/upload/upfile',
filePath: url,
name: 'file',
header:{'token':token},
formData: {from: 'app'},
success: (res) => {
let data = JSON.parse(res.data)
// console.log(data.data)
resolve(data.data)
// console.log(me.picList1)
}
});
})
},
/* 上传图片 结束 */上一篇: uniapp页面生命周期和组件生命周期
下一篇: uniapp解决H5跨域的代码