這篇文章主要介紹了微信小程序中如何實(shí)現(xiàn)拍照或從相冊(cè)選取圖片上傳,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)下花園,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
upload.wxml
<!--pages/upload/upload.wxml--> <button bindtap='uploadPhoto'>拍照選取照片上傳</button>
upload.js
// pages/upload/upload.js Page({ data: { imgData: [] }, uploadPhoto(e) { // 拍攝或從相冊(cè)選取上傳 let that = this; wx.chooseImage({ count: 1, // 默認(rèn)9 sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊(cè)還是相機(jī),默認(rèn)二者都有 success(res) { let tempFilePaths = res.tempFilePaths; // 返回選定照片的本地路徑列表 that.upload(that, tempFilePaths); } }) }, upload(page, path) { // 上傳圖片 wx.showToast({ icon: "loading", title: "正在上傳……" }); wx.uploadFile({ url: '上傳圖片接口url', //后端接口 filePath: path[0], name: 'file', header: { "Content-Type": "multipart/form-data" }, success(res) { if (res.statusCode != 200) { wx.showModal({ title: '提示', content: '上傳失敗', showCancel: false }); return; } else{ console.log("上傳成功! 可對(duì)返回的值進(jìn)行操作,比如:存入imgData;"); } }, fail(e) { wx.showModal({ title: '提示', content: '上傳失敗', showCancel: false }); }, complete() { wx.hideToast(); //隱藏Toast } }) } })
ps:以上是單圖上傳,如果需要多圖上傳,請(qǐng)看下方:
// pages/publish/publish.js Page({ data: { imgData: [] }, uploadPhoto(e) { // 拍攝或從相冊(cè)選取上傳 let that = this; wx.chooseImage({ count: 9 - that.data.imgData.length, // 默認(rèn)最多上傳9張 sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'], // 可以指定來源是相冊(cè)還是相機(jī),默認(rèn)二者都有 success(res) { let tempFilePaths = res.tempFilePaths; // 返回選定照片的本地路徑列表 that.upload(that, tempFilePaths); } }) }, upload(page, path) { // 上傳圖片 let that = this; wx.showToast({ icon: "loading", title: "正在上傳……" }); for (var i=0; i<path.length; i++) { wx.uploadFile({ url: '上傳圖片接口url', //后端接口 filePath: path[i], name: 'file', header: app.globalData.header, success(res) { if (res.statusCode != 200) { wx.showModal({ title: '提示', content: '第' + i +'張圖片上傳失敗', showCancel: false }); return; } else { console.log('第' + i +'張圖片上傳成功!可在此操作,比如:存入imgData;'); } }, fail(e) { wx.showModal({ title: '提示', content: '第' + i +'張圖片上傳失敗', showCancel: false }); }, complete() { wx.hideToast(); //隱藏Toast } }) } } })
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序中如何實(shí)現(xiàn)拍照或從相冊(cè)選取圖片上傳”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
網(wǎng)頁名稱:微信小程序中如何實(shí)現(xiàn)拍照或從相冊(cè)選取圖片上傳
URL標(biāo)題:http://jinyejixie.com/article44/iische.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、軟件開發(fā)、小程序開發(fā)、定制網(wǎng)站、網(wǎng)站營銷、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)