微信小程序?qū)崿F(xiàn)上傳多張圖片、刪除圖片
最近在做微信小程序,遇到上傳多張圖片到服務(wù)器,計算上傳圖片的張數(shù),并且可以手動刪除圖片,下面是效果圖
效果圖:
本來用的是小程序提供的 mp-uploader 上傳圖片的組件,無奈次組件刪除效果不是我想要的,只能用 wx.chooseImage進行上傳圖片,在使用uplaodFile將圖片發(fā)送給后臺服務(wù)器。
下面直接展示代碼:
wxml:
<view class="con_titles"> <view class="con_left"> <image src="../../images/comint.png"></image> <text class="titles_t">患者病歷</text> </view> <view class="img_num">{{imgShow.length}}/6</view> </view> <view class="page__bd"> <!-- <mp-uploader style='color:#353535' bindfail="uploadError" bindsuccess="uploadSuccess" select="{{selectFile}}" upload="{{uplaodFile}}" files="{{files}}" max-count="6" title="患者病歷"></mp-uploader> --> <view class="add-image"> <view class="images-boxc" wx:for="{{imgShow}}" wx:for-item="item" wx:key="image"> <image class="image_size" data-index="{{index}}" data-src="{{item}}" src="{{item}}" bindtap="clickImage"></image> <image class="delete-image" data-index="{{index}}" src="../../images/delete_img.png" bindtap="deleteImage"></image> </view> <view class="images-add" wx:if="{{imgShow.length<6}}"> <image class="image_size image_sizen" src="../../images/add_img.png" bindtap="chooseImage"></image> </view> </view> </view>
wxss:
/* 上傳圖片 */ .images-boxc { position: relative; border: dashed 1px #bfbfbf; width: 139rpx; height: 139rpx; margin-right: 32rpx; margin-bottom: 32rpx; } .delete-image { position: absolute; width: 30rpx; height: 30rpx; right: 16rpx; top: 16rpx; } .add-image { display: flex; flex-wrap: wrap; } .image_size { width: 139rpx; height: 139rpx; } .image_sizen { height: 142rpx; }
js:
data: { count: 6, //設(shè)置最多6張圖片 allImg: [], imgShow: [], }, // 上傳圖片 chooseImage: function() { wx.showLoading({ title: '加載中...', mask: true }) var that = this; var imgShow = that.data.imgShow; var count = that.data.count - imgShow.length; //設(shè)置最多6張圖片 wx.chooseImage({ count: count, sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有 success: function(res) { console.log(res) that.uplaodFile(res) for (var i = 0, h = res.tempFilePaths.length; i < h; i++) { imgShow.push(res.tempFilePaths[i]); that.setData({ imgShow: imgShow }) } wx.hideLoading({ title: '加載中...', mask: false }) } }) }, // 刪除圖片 deleteImage(e) { let self = this; let index = e.target.dataset.index; let imgShow = self.data.imgShow; let allImg = self.data.allImg; allImg.splice(index, 1); imgShow.splice(index, 1); this.setData({ imgShow: imgShow, allImg: allImg }) }, previewImage: function(e) { console.log(this.data.files) wx.previewImage({ current: e.currentTarget.id, // 當前顯示圖片的http鏈接 urls: this.data.files // 需要預(yù)覽的圖片http鏈接列表 }) }, selectFile(files) { console.log('files', files) // 返回false可以阻止某次文件上傳 }, uplaodFile(files) { console.log('upload files', files) let that = this files.tempFilePaths.forEach(element => { util.uploadFile('/fastdfsServer/fileUpload', element, 'file', {}, function(res) { //上傳本地圖片地址到服務(wù)器 返回地址 存放到input提交時取值 res = JSON.parse(res); if (res.responseCode == 0) { sysMsg.sysMsg("上傳成功", 1000, 'success'); that.setData({ allImg: that.data.allImg.concat(res.responseBody) }); } else { sysMsg.sysMsg("上傳失敗", 1500, 'error'); } }); }); // 文件上傳的函數(shù),返回一個promise return new Promise((resolve, reject) => { resolve({ urls: files.tempFilePaths }); setTimeout(() => { reject('some error') }, 10000) }) }, uploadError(e) { console.log('upload error', e.detail) }, uploadSuccess(e) { // this.setData({ // allImg: this.data.allImg.concat(e.detail.urls[0]) // }); console.log('upload success', e.detail, e.detail.urls) },
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript showModalDialog,open取得父窗口的方法
showModalDialog,open取得父窗口的代碼,需要的朋友可以參考下。2010-03-03基于Node.js的JavaScript項目構(gòu)建工具gulp的使用教程
也許你使用過grunt,那么這里來安利gulp的話就更加不會陌生了,下面我們就來看一下基于Node.js的JavaScript項目構(gòu)建工具gulp的使用教程2016-05-05javascript實現(xiàn)狀態(tài)欄文字首尾相接循環(huán)滾動的方法
這篇文章主要介紹了javascript實現(xiàn)狀態(tài)欄文字首尾相接循環(huán)滾動的方法,實例分析了javascript定時函數(shù)及頁面元素屬性操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07JavaScript 選中文字并響應(yīng)獲取的實現(xiàn)代碼
當鼠標選擇一段文字時,對這個事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。2011-08-08