微信小程序?qū)崿F(xiàn)一張或多張圖片上傳(云開發(fā))
一、簡介:
這篇文章向大家展示的是把圖片上傳到云數(shù)據(jù)庫中,這是我做商城項(xiàng)目時(shí)研究的。大家都知道,云開發(fā)是沒有后端開發(fā)的,所有圖片我們要放到云數(shù)據(jù)庫中。
二、素材圖:
:
三、效果圖:
四、代碼:
wxml:
<!--miniprogram/pages/fb/fb.wxml--> <view class='pages'> <view class='top'><text class='top_name'>商品圖片:</text></view> <!-- 圖片 --> <view class="images_box"> <block wx:key="imgbox" wx:for="{{imgbox}}"> <view class='img-box'> <image class='img' src='{{item}}'></image> <view class='img-delect' data-deindex='{{index}}' bindtap='imgDelete1'> <image class='img' src='../../images/delect.png'></image> </view> </view> </block> <view class='img-box' bindtap='addPic1' wx:if="{{imgbox.length<9}}"> <image class='img' src='../../images/add_image.png'></image> </view> </view> <button bindtap='fb'>上傳圖片</button> </view>
wxss:
/* miniprogram/pages/fb/fb.wxss */ page{ background-color: rgba(200, 198, 201, 0.527); } .pages{ width: 98%; margin: auto; overflow: hidden; } .top{ width: 100%; overflow: hidden; margin: auto; font-size: 50rpx; background-color: white; border-left: 8rpx solid rgb(9, 245, 60); border-bottom: 1rpx solid rgba(117, 116, 116, 0.527); } .top_name{ margin-left: 20rpx; } /* 圖片 */ .images_box{ width: 100%; display: flex; flex-direction: row; flex-wrap: wrap; justify-content: flex-start; background-color: white; } .img-box{ border: 5rpx; border-style: solid; border-color: rgba(0, 0, 0, 0.452); width: 200rpx; height: 200rpx; margin-left: 35rpx; margin-top: 20rpx; margin-bottom: 20rpx; position: relative; } /* 刪除圖片 */ .img-delect{ width:50rpx; height:50rpx; border-radius:50%; position:absolute; right:-20rpx; top:-20rpx; } .img{ width: 100%; height: 100%; } .jiage{ height: 60rpx; width: 90%; margin-left: 5%; margin-right: 5%; background-color: white; display: flex; justify-content: flex-start; } .rmb{ width: 280rpx; border: 2rpx solid rgb(199, 197, 197); } button{ margin-top: 20rpx; background-color: green; } .radio-group{ display: flex; }
js:
// pages/fb/fb.js const app = getApp() const db = wx.cloud.database();//初始化數(shù)據(jù)庫 Page({ /** * 頁面的初始數(shù)據(jù) */ data: { imgbox: [],//選擇圖片 fileIDs: [],//上傳云存儲(chǔ)后的返回值 }, // 刪除照片 && imgDelete1: function (e) { let that = this; let index = e.currentTarget.dataset.deindex; let imgbox = this.data.imgbox; imgbox.splice(index, 1) that.setData({ imgbox: imgbox }); }, // 選擇圖片 &&& addPic1: function (e) { var imgbox = this.data.imgbox; console.log(imgbox) var that = this; var n = 5; if (5 > imgbox.length > 0) { n = 5 - imgbox.length; } else if (imgbox.length == 5) { n = 1; } wx.chooseImage({ count: n, // 默認(rèn)9,設(shè)置圖片張數(shù) sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機(jī),默認(rèn)二者都有 success: function (res) { // console.log(res.tempFilePaths) // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片 var tempFilePaths = res.tempFilePaths if (imgbox.length == 0) { imgbox = tempFilePaths } else if (5 > imgbox.length) { imgbox = imgbox.concat(tempFilePaths); } that.setData({ imgbox: imgbox }); } }) }, //圖片 imgbox: function (e) { this.setData({ imgbox: e.detail.value }) }, //發(fā)布按鈕 fb: function (e) { if (!this.data.imgbox.length) { wx.showToast({ icon: 'none', title: '圖片類容為空' }); } else { //上傳圖片到云存儲(chǔ) wx.showLoading({ title: '上傳中', }) let promiseArr = []; for (let i = 0; i < this.data.imgbox.length; i++) { promiseArr.push(new Promise((reslove, reject) => { let item = this.data.imgbox[i]; let suffix = /\.\w+$/.exec(item)[0];//正則表達(dá)式返回文件的擴(kuò)展名 wx.cloud.uploadFile({ cloudPath: new Date().getTime() + suffix, // 上傳至云端的路徑 filePath: item, // 小程序臨時(shí)文件路徑 success: res => { this.setData({ fileIDs: this.data.fileIDs.concat(res.fileID) }); console.log(res.fileID)//輸出上傳后圖片的返回地址 reslove(); wx.hideLoading(); wx.showToast({ title: "上傳成功", }) }, fail: res=>{ wx.hideLoading(); wx.showToast({ title: "上傳失敗", }) } }) })); } Promise.all(promiseArr).then(res => {//等數(shù)組都做完后做then方法 console.log("圖片上傳完成后再執(zhí)行") this.setData({ imgbox:[] }) }) } }, })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)上傳照片代碼實(shí)例解析
- uni-app開發(fā)微信小程序之H5壓縮上傳圖片的問題詳解
- 微信小程序?qū)崿F(xiàn)云開發(fā)上傳文件、圖片功能
- 微信小程序?qū)崿F(xiàn)多文件或者圖片上傳
- 微信小程序?qū)崿F(xiàn)上傳圖片
- 微信小程序圖片上傳組件實(shí)現(xiàn)圖片拖拽排序
- 微信小程序?qū)崿F(xiàn)上傳圖片的功能
- 微信小程序?qū)崿F(xiàn)上傳多張圖片、刪除圖片
- 微信小程序?qū)崿F(xiàn)同時(shí)上傳多張圖片
- 微信小程序?qū)崿F(xiàn)文件、圖片上傳功能
- 微信小程序?qū)崿F(xiàn)多張照片上傳功能
相關(guān)文章
JavaScript實(shí)現(xiàn)網(wǎng)頁留言板功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁留言板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11推薦三款不錯(cuò)的圖片壓縮上傳插件(webuploader、localResizeIMG4、LUploader)
這篇文章主要為大家詳細(xì)介紹了三款不錯(cuò)的圖片壓縮上傳插件,webuploader、移動(dòng)端上傳插件localResizeIMG4以及LUploader)2017-04-04javascript中IE瀏覽器不支持NEW DATE()帶參數(shù)的解決方法
在火狐下 可以正常取得時(shí)間,在IE7下 卻是 NaN。糾結(jié)老長時(shí)間,放棄了new date 然后再老外的論壇中找了一段段代碼可以兼容所有瀏覽器的格式化日期代碼2012-03-03利用js實(shí)現(xiàn)Ajax并發(fā)請求限制請求數(shù)量的示例代碼
這篇文章主要介紹了利用js實(shí)現(xiàn)Ajax并發(fā)請求限制請求數(shù)量的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04javascript模板方法模式和職責(zé)鏈模式實(shí)例分析
這篇文章主要介紹了javascript模板方法模式和職責(zé)鏈模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了模板方法模式和職責(zé)鏈模式基本原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2023-07-07在js(jquery)中獲得文本框焦點(diǎn)和失去焦點(diǎn)的方法
文章介紹兩個(gè)方法和種是利用javascript onFocus onBlur來判斷焦點(diǎn)和失去焦點(diǎn),加一種是利用jquery $("p").blur(); 或$("p").blur(fn)來實(shí)現(xiàn),有需要的朋友可以參考一下2012-12-12JavaScript中 ES6 generator數(shù)據(jù)類型詳解
generator 是ES6引入的新的數(shù)據(jù)類型,由function* 定義, (注意*號(hào)),接下來通過本文給大家介紹js中 ES6 generator數(shù)據(jù)類型,非常不錯(cuò),感興趣的朋友一起學(xué)習(xí)吧2016-08-08JavaScript常用截取字符串的三種方式用法區(qū)別實(shí)例解析
本文給大家分享JavaScript常用截取字符串的三種方式及每種用法的區(qū)別解析,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05