- 在wxml文件中添加一個(gè)按鈕和一個(gè)image標(biāo)簽用于顯示上傳的圖片
<view> <button bindtap="chooseImage">選擇圖片</button> <image src="{{imageUrl}}" /> </view>
- 在js文件中添加選擇圖片和上傳圖片的方法
Page({ data: { imageUrl: '' }, chooseImage() { wx.chooseImage({ count: 1, // 可選擇的圖片數(shù)量 sizeType: ['compressed'], // 壓縮圖片 sourceType: ['album', 'camera'], // 來源:相冊(cè)或相機(jī) success: (res)=> { // 將選擇的圖片上傳到服務(wù)器 this.uploadImage(res.tempFilePaths[0]); } }) }, uploadImage(imagePath) { wx.uploadFile({ url: '服務(wù)器地址', // 上傳圖片的接口地址 filePath: imagePath, // 圖片文件路徑 name: 'image', // 文件對(duì)應(yīng)的 key , 開發(fā)者在服務(wù)器端通過這個(gè) key 可以獲取到文件二進(jìn)制內(nèi)容 success: (res) => { // 上傳成功后,將服務(wù)器返回的圖片地址更新到image標(biāo)簽中 this.setData({ imageUrl: res.data }); }, fail: function (res) { console.log(res); } }) } })
文章來源地址http://www.zghlxwxcb.cn/news/detail-415324.html
文章來源:http://www.zghlxwxcb.cn/news/detail-415324.html
到了這里,關(guān)于微信小程序?qū)崿F(xiàn)圖片上傳(清晰版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!