在社区功能中,想要发布一个帖子,那么最好在本地预览得到图片数量然后通过循环一次上传,而不是直接上传.这里对组件进行了一些修改.
html:
<van-uploader
:disabled="disable"
:file-list="previewList"
@after-read="previewImg"
max-count="9" />
js:
data() {
return {
text: "",
school: "",
disable: true,
postId: "",
fileList: [],
previewList: [],
imgId: 0,
file: null
}
},
methods:{
previewImg(e) {
const {
previewFile
} = e.detail
this.previewList.push({
...previewFile,
url: e.detail.file.url
})
},
submit() {
wx.showLoading({
title: "开始上传..."
})
let self = this
uni.request({
url: getApp().globalData.baseURL + "/post/addAPost",
data: {
"openid": wx.getStorageSync('openid'),
"school": self.school,
"content": self.text
},
header: {
"token": wx.getStorageSync('token')
},
success: (res) => {
let self = this
self.postId = res.data.data
let length = self.previewList.length
if (length !== 0) {
for (let i = 0; i < length; i++) {
wx.uploadFile({
url: getApp().globalData.baseURL + "/post/addAPostImage",
filePath: self.previewList[i].url,
name: "file",
formData: {
"file": self.previewList[i],
"imageId": i,
"postId": self.postId
},
header: {
"token": wx.getStorageSync('token')
},
success: (res) => {
self.fileList.push({
...self.previewList[i],
url: res.data
});
if (self.previewList.