微信小程序上傳圖片到服務(wù)器實(shí)例代碼
上傳圖片到服務(wù)器:
1.先在前端寫(xiě)一個(gè)選擇圖片的區(qū)域來(lái)觸發(fā)wx.chooseImage接口并用wx.setStorage接口把圖片路徑存起來(lái)。

-wxml
<view class="shangchuan" bindtap="choose">
<image style="width:100%;height:100%;" src="{{tempFilePaths}}"></image>
</view>
<button formType='submit' class="fabu">發(fā)布項(xiàng)目</button>
/**選擇圖片 */
choose: function () {
var that = this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album', 'camera'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有
success: function (res) {
var tempFilePaths = res.tempFilePaths
that.setData({
tempFilePaths: res.tempFilePaths
})
console.log(res.tempFilePaths)
wx.setStorage({ key: "card", data: tempFilePaths[0] })
}
})
},
2.使用wx.uploadFile將剛才上傳的圖片上傳到服務(wù)器上
formSubmit2: function (e) {
var that = this
var card = wx.getStorageSync('card')
wx.uploadFile({
url: app.globalData.create_funds,
filePath: card,
name: 'card',
formData: {
'user_id': app.globalData.user_id,
'person': e.detail.value.person,
'company': e.detail.value.company,
},
success: function (res) {
console.log(res)
}
})
}
}
},
PS: 微信小程序上傳一或多張圖片
一.要點(diǎn)
1.選取圖片
wx.chooseImage({
sizeType: [], // original 原圖,compressed 壓縮圖,默認(rèn)二者都有
sourceType: [], // album 從相冊(cè)選圖,camera 使用相機(jī),默認(rèn)二者都有
success: function (res) {
console.log(res);
var array = res.tempFilePaths, //圖片的本地文件路徑列表
}
})
2.上傳圖片
wx.uploadFile({
url: '', //開(kāi)發(fā)者服務(wù)器的 url
filePath: '', // 要上傳文件資源的路徑 String類型?。?!
name: 'uploadFile', // 文件對(duì)應(yīng)的 key ,(后臺(tái)接口規(guī)定的關(guān)于圖片的請(qǐng)求參數(shù))
header: {
'content-type': 'multipart/form-data'
}, // 設(shè)置請(qǐng)求的 header
formData: { }, // HTTP 請(qǐng)求中其他額外的參數(shù)
success: function (res) {
},
fail: function (res) {
}
})
二.代碼示例
// 點(diǎn)擊上傳圖片
upShopLogo: function () {
var that = this;
wx.showActionSheet({
itemList: ['從相冊(cè)中選擇', '拍照'],
itemColor: "#f7982a",
success: function (res) {
if (!res.cancel) {
if (res.tapIndex == 0) {
that.chooseWxImageShop('album')
} else if (res.tapIndex == 1) {
that.chooseWxImageShop('camera')
}
}
}
})
},
chooseWxImageShop: function (type) {
var that = this;
wx.chooseImage({
sizeType: ['original', 'compressed'],
sourceType: [type],
success: function (res) {
/*上傳單張
that.data.orderDetail.shopImage = res.tempFilePaths[0],
that.upload_file(API_URL + 'shop/shopIcon', res.tempFilePaths[0])
*/
/*上傳多張(遍歷數(shù)組,一次傳一張)
for (var index in res.tempFilePaths) {
that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])
}
*/
}
})
},
upload_file: function (url, filePath) {
var that = this;
wx.uploadFile({
url: url,
filePath: filePath,
name: 'uploadFile',
header: {
'content-type': 'multipart/form-data'
}, // 設(shè)置請(qǐng)求的 header
formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 請(qǐng)求中其他額外的 form data
success: function (res) {
wx.showToast({
title: "圖片修改成功",
icon: 'success',
duration: 700
})
},
fail: function (res) {
}
})
},
總結(jié)
以上所述是小編給大家介紹的微信小程序上傳圖片到服務(wù)器實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JS簡(jiǎn)單循環(huán)遍歷json數(shù)組的方法
這篇文章主要介紹了JS簡(jiǎn)單循環(huán)遍歷json數(shù)組的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了JavaScript循環(huán)遍歷json數(shù)組的方法,并提供了jQuery遍歷json的方法,需要的朋友可以參考下2016-04-04
for 循環(huán)性能比較 提高for循環(huán)的效率
性能有較大的區(qū)別嗎,有必要采取這種寫(xiě)法嗎2009-03-03
基于JavaScript實(shí)現(xiàn)多級(jí)菜單效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)多級(jí)菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
javascript系統(tǒng)時(shí)間設(shè)置操作示例
這篇文章主要介紹了javascript系統(tǒng)時(shí)間設(shè)置操作,涉及javascript時(shí)間運(yùn)算與判斷相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
JavaScript必備的斷點(diǎn)調(diào)試技巧總結(jié)(推薦)
打斷點(diǎn)操作很簡(jiǎn)單,核心的問(wèn)題在于,斷點(diǎn)怎么打才能夠排查出代碼的問(wèn)題所在呢?下面這篇文章主要給大家總結(jié)介紹了關(guān)于JavaScript必備的斷點(diǎn)調(diào)試技巧,需要的朋友可以參考下2021-09-09
uniapp內(nèi)置組件scroll-view案例詳解(完整代碼)
這篇文章主要介紹了uniapp內(nèi)置組件scroll-view案例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
微信小程序 連續(xù)旋轉(zhuǎn)動(dòng)畫(huà)(this.animation.rotate)詳解
這篇文章主要介紹了微信小程序 連續(xù)旋轉(zhuǎn)動(dòng)畫(huà)(this.animation.rotate)詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
最新JS正則表達(dá)式驗(yàn)證郵箱和手機(jī)號(hào)實(shí)例(2022)
在前端開(kāi)發(fā)過(guò)程中,通過(guò)使用JS的正則表達(dá)式來(lái)校驗(yàn)輸入的郵箱或者手機(jī)號(hào)是否正確,這也是一個(gè)非常常見(jiàn)的業(yè)務(wù)情景需求,下面這篇文章主要給大家介紹了關(guān)于利用JS正則表達(dá)式驗(yàn)證郵箱和手機(jī)號(hào)的相關(guān)資料,需要的朋友可以參考下2022-08-08
javascript實(shí)現(xiàn)手機(jī)震動(dòng)API代碼
一個(gè)新的API出來(lái)了。HTML5 (很快)將支持用戶設(shè)備振動(dòng)。這明顯是很有趣的事情,比如它可以用戶觸發(fā)提醒,提升游戲體驗(yàn),下面小編給大家整理javascript手機(jī)震動(dòng)api,需要的朋友可以參考下2015-08-08
Js實(shí)現(xiàn)當(dāng)前點(diǎn)擊a標(biāo)簽變色突出顯示其他a標(biāo)簽回復(fù)原色
當(dāng)一個(gè)頁(yè)面有多個(gè)a標(biāo)簽,實(shí)現(xiàn)當(dāng)前點(diǎn)擊a標(biāo)簽變色,其他a標(biāo)簽回復(fù)原色,具體實(shí)現(xiàn)如下,喜歡的朋友可以參考下2013-11-11

