微信小程序?qū)崿F(xiàn)圖片上傳
更新時間:2019年05月23日 10:20:56 作者:前端_李嘉豪
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)圖片上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下/p>
圖片上傳服務(wù)器:
wxml
<view class="container"> <button bindtap='chooseImageTap'>上傳圖片</button> </view>
wxss
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
imgs: [],//本地圖片地址數(shù)組
picPaths:[],//網(wǎng)絡(luò)路徑
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
//添加上傳圖片
chooseImageTap: function () {
var that = this;
wx.showActionSheet({
itemList: ['從相冊中選擇', '拍照'],
itemColor: "#00000",
success: function (res) {
if (!res.cancel) {
if (res.tapIndex == 0) {
that.chooseWxImage('album')
} else if (res.tapIndex == 1) {
that.chooseWxImage('camera')
}
}
}
})
},
// 圖片本地路徑
chooseWxImage: function (type) {
var that = this;
var imgsPaths = that.data.imgs;
wx.chooseImage({
sizeType: ['original', 'compressed'],
sourceType: [type],
success: function (res) {
console.log(res.tempFilePaths[0]);
that.upImgs(res.tempFilePaths[0], 0) //調(diào)用上傳方法
}
})
},
//上傳服務(wù)器
upImgs: function (imgurl, index) {
var that = this;
wx.uploadFile({
url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxx',//
filePath: imgurl,
name: 'file',
header: {
'content-type': 'multipart/form-data'
},
formData: null,
success: function (res) {
console.log(res) //接口返回網(wǎng)絡(luò)路徑
var data = JSON.parse(res.data)
that.data.picPaths.push(data['msg'])
that.setData({
picPaths: that.data.picPaths
})
console.log(that.data.picPaths)
}
})
},
})
思路很簡單,多張上傳的話,在 upImgs 方法回調(diào)做判斷 index++ 繼續(xù)調(diào)用 upImgs方法即可
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于 webpack2 實現(xiàn)的多入口項目腳手架詳解
這篇文章主要給大家介紹了基于 webpack2 實現(xiàn)的多入口項目腳手架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
詳解JavaScript Promise和Async/Await
這篇文章主要介紹了JavaScript Promise和Async/Await,對異步編程感興趣的同學(xué),可以參考下2021-04-04
JS中for...in?和?for...of?的區(qū)別解析
for?…?in?用于迭代對象的可枚舉字符串屬性,包括自身屬性和繼承的屬性,但不會遍歷對象的原型鏈上的?非可枚舉屬性,以及對象的方法,這篇文章主要介紹了JS中for...in?和?for...of?的區(qū)別,需要的朋友可以參考下2024-03-03
baidu博客的編輯友情鏈接的新的層窗口!經(jīng)典~支持【FF】
baidu博客的編輯友情鏈接的新的層窗口!經(jīng)典~支持【FF】...2007-02-02

