微信小程序?qū)崿F(xiàn)上傳圖片的功能
更新時間:2021年08月13日 17:25:21 作者:半度℃溫?zé)?
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)上傳圖片的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序?qū)崿F(xiàn)上傳圖片的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
WXML
<view class="img-wrap"> <view class="txt">上傳圖片</view> <view class="imglist"> <view class="item" wx:for="{{imgs}}" wx:key="item"> <image src="{{item}}" alt=""></image> <view class='delete' bindtap='deleteImg' data-index="{{index}}"> <image src="../../../images/icon.png"></image> </view> </view> <view class="last-item" wx:if="{{imgs.length >= 3 ? false : true}}" bindtap="bindUpload"> <text class="sign">+</text> </view> </view> </view>
JS
data: { imgs: [], count: 3 }, bindUpload: function (e) { switch (this.data.imgs.length) { case 0: this.data.count = 3 break case 1: this.data.count = 2 break case 2: this.data.count = 1 break } var that = this wx.chooseImage({ count: that.data.count, // 默認(rèn)3 sizeType: ["original", "compressed"], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ["album", "camera"], // 可以指定來源是相冊還是相機,默認(rèn)二者都有 success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片 var tempFilePaths = res.tempFilePaths for (var i = 0; i < tempFilePaths.length; i++) { wx.uploadFile({ url: 'https://graph.baidu.com/upload', filePath: tempFilePaths[i], name: "file", header: { "content-type": "multipart/form-data" }, success: function (res) { if (res.statusCode == 200) { wx.showToast({ title: "上傳成功", icon: "none", duration: 1500 }) that.data.imgs.push(JSON.parse(res.data).data) that.setData({ imgs: that.data.imgs }) } }, fail: function (err) { wx.showToast({ title: "上傳失敗", icon: "none", duration: 2000 }) }, complete: function (result) { console.log(result.errMsg) } }) } } }) }, // 刪除圖片 deleteImg: function (e) { var that = this wx.showModal({ title: "提示", content: "是否刪除", success: function (res) { if (res.confirm) { for (var i = 0; i < that.data.imgs.length; i++) { if (i == e.currentTarget.dataset.index) that.data.imgs.splice(i, 1) } that.setData({ imgs: that.data.imgs }) } else if (res.cancel) { console.log("用戶點擊取消") } } }) }
WXSS
.wrap { width: 100%; padding: 0 30rpx; box-sizing: border-box; } .wrap .img-wrap { font-size: 30rpx; color: #33373E; margin-bottom: 10rpx; } .wrap .img-wrap .txt { margin-bottom: 20rpx; } .wrap .img-wrap .imglist { display: flex; flex-wrap: wrap; } .wrap .img-wrap .imglist .item { width: 150rpx; height: 150rpx; margin-right: 22rpx; margin-bottom: 10rpx; position: relative; } .wrap .img-wrap .imglist .last-item { width: 150rpx; height: 150rpx; text-align: center; line-height: 146rpx; border: 2rpx dashed #8B97A9; box-sizing: border-box; } .wrap .img-wrap .imglist .item image { width: 100%; height: 100%; } .wrap .img-wrap .imglist .item .delete { width: 30rpx; height: 30rpx; position: absolute; top: -14rpx; right: -12rpx; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 微信小程序?qū)崿F(xiàn)上傳照片代碼實例解析
- uni-app開發(fā)微信小程序之H5壓縮上傳圖片的問題詳解
- 微信小程序?qū)崿F(xiàn)云開發(fā)上傳文件、圖片功能
- 微信小程序?qū)崿F(xiàn)多文件或者圖片上傳
- 微信小程序?qū)崿F(xiàn)上傳圖片
- 微信小程序圖片上傳組件實現(xiàn)圖片拖拽排序
- 微信小程序?qū)崿F(xiàn)上傳多張圖片、刪除圖片
- 微信小程序?qū)崿F(xiàn)同時上傳多張圖片
- 微信小程序?qū)崿F(xiàn)一張或多張圖片上傳(云開發(fā))
- 微信小程序?qū)崿F(xiàn)文件、圖片上傳功能
- 微信小程序?qū)崿F(xiàn)多張照片上傳功能
相關(guān)文章
JavaScript使用forEach()與jQuery使用each遍歷數(shù)組時return false 的區(qū)別
這篇文章主要介紹了JavaScript使用forEach()與jQuery使用each遍歷數(shù)組時return false 的區(qū)別,非常不錯,需要的朋友可以參考下2016-08-08驗證用戶是否修改過頁面的數(shù)據(jù)的實現(xiàn)方法
在實現(xiàn)程序的修改模塊時,要在頁面端檢查用戶是否修改過數(shù)據(jù),以便提醒用戶及時保存修改后的數(shù)據(jù)。2008-09-09微信小程序?qū)崿F(xiàn)事件傳參與數(shù)據(jù)同步流程詳解
這篇文章主要介紹了微信小程序開發(fā)中實現(xiàn)事件傳參與數(shù)據(jù)同步的詳細(xì)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10javascript貪吃蛇游戲設(shè)計與實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了javascript貪吃蛇游戲設(shè)計與實現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09JS根據(jù)Unix時間戳顯示發(fā)布時間是多久前【項目實測】
小編最近在實現(xiàn)這樣的需求類似微信朋友圈顯示發(fā)布時間為距離當(dāng)前時間多久之前這樣的功能,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2019-07-07