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