溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何實(shí)現(xiàn)上傳圖片到服務(wù)器

發(fā)布時(shí)間:2021-07-06 09:23:15 來(lái)源:億速云 閱讀:887 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹了微信小程序如何實(shí)現(xiàn)上傳圖片到服務(wù)器,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

上傳圖片到服務(wù)器:

1.先在前端寫(xiě)一個(gè)選擇圖片的區(qū)域來(lái)觸發(fā)wx.chooseImage接口并用wx.setStorage接口把圖片路徑存起來(lái)。

微信小程序如何實(shí)現(xiàn)上傳圖片到服務(wù)器

-wxml
 <view class="shangchuan" bindtap="choose">
  <image  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) {
   }
  })
 },

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序如何實(shí)現(xiàn)上傳圖片到服務(wù)器”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI