本文小編為大家詳細介紹“微信小程序怎么實現多文件或圖片上傳”,內容詳細,步驟清晰,細節處理妥當,希望這篇“微信小程序怎么實現多文件或圖片上傳”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
html
<view class="list1"> <view class="fonts">上傳事件相關資料或文件(jpg/pdf/word)<text class="xuan">(選填)</text> </view> <view class="cell"> <view class='jinList' wx:for="{{uploaderList}}" wx:key="index"> <image class="close1" bindtap="close" data-index="{{index}}" src="../../images/close1.png"></image> <image class="jinListImg" wx:if="{{type != 'file'}}" bindtap='showImg' data-index="{{index}}" src='{{item}}'></image> <image class="jinListImg" src="../../images/img.png" wx:if="{{type == 'file'}}" mode="aspectFill"></image> </view> <view class="jia jia1" bindtap="upImg"> <image src="../../images/jia.png" wx:if="{{isHidden}}"></image> </view> </view> </view>
js
data: { isHidden: true,//原始添加 url: [],//上傳文件路徑 type: '',//上傳文件類型 uploaderList: [],//上傳文件數組 showModal: false, }, // // 上傳圖片 upImg() { var that = this wx.chooseMessageFile({ count: 1, type: 'all', success(res) { // tempFilePath可以作為img標簽的src屬性顯示圖片 // console.log('vvvvvvvv',res.tempFiles) const tempFilePaths = res.tempFiles[0].path const type = res.tempFiles[0].type wx.uploadFile({ url: app.globalData.urlSrc + '/api/chuan/index', filePath: tempFilePaths, name: 'file', success(res) { const datas = JSON.parse(res.data) console.log('上傳文件', datas) var status = datas.status that.data.list if (status == 1) { var upFiles = datas.data if (upFiles != '') { let tempFile = tempFilePaths; let uploaderList = that.data.uploaderList.concat(tempFile);//返回頁面的圖片數據 that.data.url = that.data.url.concat(upFiles);//傳給后臺的圖片數據 that.setData({ type: type, uploaderList: uploaderList }) } } } }) } }) }, // 刪除上傳 close(e) { var that = this var nowList = [];//新數據 var uploaderList = that.data.uploaderList;//原數據 for (let i = 0; i < uploaderList.length; i++) { if (i == e.currentTarget.dataset.index) { continue; } else { nowList.push(uploaderList[i]) } } that.setData({ uploaderList: nowList, isHidden: true }) },
css
.list1 { width: 670rpx; margin: 0 auto; } .tops { display: flex; margin-bottom: 22rpx; } .left1 { margin-left: 12rpx; line-height: 44rpx; text-align: left; } .listImg1 { width: 44rpx; height: 44rpx; margin-left: 24rpx; } .textarea1 { width: 670rpx; height: 250rpx; line-height: 40rpx; border-radius: 44rpx; background-color: rgba(255, 255, 255, 1); color: rgba(16, 16, 16, 1); font-size: 14px; text-align: left; border: 1px solid rgba(240, 240, 240, 1); margin:0 auto; padding: 20rpx 40rpx; box-sizing: border-box; margin-bottom: 40rpx; } .jia{ width: 140rpx; height: 140rpx; margin-top: 40rpx; } .jia image{ width: 140rpx; height: 140rpx; } .cell { width: 100%; overflow: hidden; } .jinListImg { width: 140rpx; height: 140rpx; border-radius: 24rpx; } .close1 { width: 40rpx; height: 40rpx; position: absolute; margin-left: 100rpx; } .jia1 { float: left; position: relative; }
示例圖
讀到這里,這篇“微信小程序怎么實現多文件或圖片上傳”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。