知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序?qū)崿F(xiàn)上傳多張圖片、刪除圖片
發(fā)表時(shí)間:2021-1-11
發(fā)布人:葵宇科技
瀏覽次數(shù):79
最近在做微信小程序,遇到上傳多張圖片到服務(wù)器,計(jì)算上傳圖片的張數(shù),并且可以手動(dòng)刪除圖片,下面是效果圖
效果圖:
本來用的是小程序提供的 mp-uploader 上傳圖片的組件,無奈次組件刪除效果不是我想要的,只能用 wx.chooseImage進(jìn)行上傳圖片,在使用uplaodFile將圖片發(fā)送給后臺(tái)服務(wù)器。
下面直接展示代碼:
wxml:
<view class="con_titles"> <view class="con_left"> <image src="http://www.wxapp-union.com/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="http://www.wxapp-union.com/{{item}}" src="http://www.wxapp-union.com/{{item}}" bindtap="clickImage"></image> <image class="delete-image" data-index="{{index}}" src="http://www.wxapp-union.com/images/delete_img.png" bindtap="deleteImage"></image> </view> <view class="images-add" wx:if="{{imgShow.length<6}}"> <image class="image_size image_sizen" src="http://www.wxapp-union.com/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'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊(cè)還是相機(jī),默認(rèn)二者都有 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, // 當(dāng)前顯示圖片的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提交時(shí)取值 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ù),返回一個(gè)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) },
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
相關(guān)案例查看更多
相關(guān)閱讀
- 微信小程序
- 百度小程序開發(fā)公司
- 云南網(wǎng)站建設(shè)價(jià)格
- 安家微信小程序
- 云南小程序開發(fā)哪家好
- 前端技術(shù)
- 網(wǎng)站優(yōu)化哪家好
- 云南網(wǎng)站設(shè)計(jì)
- 小程序技術(shù)
- 商標(biāo)注冊(cè)
- 小程序退款
- 網(wǎng)站制作
- 小程序密鑰
- 昆明小程序開發(fā)聯(lián)系方式
- 江蘇小程序開發(fā)
- 云南小程序定制
- 公眾號(hào)模板消息
- 云南軟件設(shè)計(jì)
- 網(wǎng)站建設(shè)需要多少錢
- 汽車報(bào)廢管理系統(tǒng)
- 百度小程序開發(fā)
- 云南網(wǎng)站建設(shè)方案 doc
- 網(wǎng)站小程序
- 云南建設(shè)廳網(wǎng)站
- 云南小程序設(shè)計(jì)
- 二叉樹
- 云南建設(shè)廳網(wǎng)站首頁
- 百度自然排名
- 網(wǎng)站建設(shè)靠譜公司
- 做小程序被騙