搜尋
首頁微信小程式小程式開發關於微信小程式上傳圖片到伺服器的程式碼

關於微信小程式上傳圖片到伺服器的程式碼

Jun 23, 2018 am 11:06 AM
上傳微信小程式

這篇文章主要介紹了微信小程式上傳圖片到伺服器的實例程式碼,在文章給大家補充介紹了微信小程式上傳一或多張圖片 的方法,本文給大家介紹的非常詳細,具有參考借鑒加載,需要的朋友可以參考下

上傳圖片到伺服器:

1.先在前端寫一個選擇圖片的區域來觸發wx.chooseImage介面並用wx.setStorage介面把圖片路徑存起來。

關於微信小程式上傳圖片到伺服器的程式碼

-wxml
 <view class="shangchuan" bindtap="choose">
  <image style="width:100%;height:100%;" src="{{tempFilePaths}}"></image>
 </view>
 <button formType=&#39;submit&#39; class="fabu">发布项目</button>
 /**选择图片 */
 choose: function () {
  var that = this
  wx.chooseImage({
   count: 1,
   sizeType: [&#39;original&#39;, &#39;compressed&#39;], // 可以指定是原图还是压缩图,默认二者都有
   sourceType: [&#39;album&#39;, &#39;camera&#39;], // 可以指定来源是相册还是相机,默认二者都有
   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將剛才上傳的圖片上傳到伺服器上

#
 formSubmit2: function (e) {
    var that = this
    var card = wx.getStorageSync(&#39;card&#39;)
    wx.uploadFile({
     url: app.globalData.create_funds,
     filePath: card,
     name: &#39;card&#39;,
     formData: {
      &#39;user_id&#39;: app.globalData.user_id,
      &#39;person&#39;: e.detail.value.person,
      &#39;company&#39;: e.detail.value.company,
     },
     success: function (res) {
      console.log(res)
     }
    })
   }
  }
 },

PS:微信小程式上傳一或多張圖片

一.重點

1.選取圖片

#
wx.chooseImage({
   sizeType: [], // original 原图,compressed 压缩图,默认二者都有
   sourceType: [], // album 从相册选图,camera 使用相机,默认二者都有
   success: function (res) {
    console.log(res);
    var array = res.tempFilePaths, //图片的本地文件路径列表
   }
  })

2.上傳圖片

wx.uploadFile({
   url: &#39;&#39;, //开发者服务器的 url
   filePath: &#39;&#39;, // 要上传文件资源的路径 String类型!!!
   name: &#39;uploadFile&#39;, // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
   header: {
    &#39;content-type&#39;: &#39;multipart/form-data&#39;
   }, // 设置请求的 header
   formData: { }, // HTTP 请求中其他额外的参数
   success: function (res) {
   },
   fail: function (res) {
   }
  })

二.程式碼範例

// 点击上传图片
upShopLogo: function () {
  var that = this;
  wx.showActionSheet({
   itemList: [&#39;从相册中选择&#39;, &#39;拍照&#39;],
   itemColor: "#f7982a",
   success: function (res) {
    if (!res.cancel) {
     if (res.tapIndex == 0) {
      that.chooseWxImageShop(&#39;album&#39;)  
     } else if (res.tapIndex == 1) {
      that.chooseWxImageShop(&#39;camera&#39;)
     }
    }
   }
  })
 },
 chooseWxImageShop: function (type) {
  var that = this;
  wx.chooseImage({
   sizeType: [&#39;original&#39;, &#39;compressed&#39;],
   sourceType: [type],
   success: function (res) {
/*上传单张
    that.data.orderDetail.shopImage = res.tempFilePaths[0],
    that.upload_file(API_URL + &#39;shop/shopIcon&#39;, res.tempFilePaths[0])
*/
 /*上传多张(遍历数组,一次传一张)
    for (var index in res.tempFilePaths) {
       that.upload_file(API_URL + &#39;shop/shopImage&#39;, res.tempFilePaths[index])
    }
*/
   }
  })
 },
upload_file: function (url, filePath) {
  var that = this;
  wx.uploadFile({
   url: url,
   filePath: filePath,
   name: &#39;uploadFile&#39;,
   header: {
    &#39;content-type&#39;: &#39;multipart/form-data&#39;
   }, // 设置请求的 header
   formData: { &#39;shopId&#39;: wx.getStorageSync(&#39;shopId&#39;) }, // HTTP 请求中其他额外的 form data
   success: function (res) {
    wx.showToast({
       title: "图片修改成功",
       icon: &#39;success&#39;,
       duration: 700
      })
   },
   fail: function (res) {
   }
  })
 },

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦

微信小程式實作點擊按鈕修改字型顏色的功能

微信小程式之取得目前位置經緯度以及地圖顯示

微信小程式之多檔案下載的簡單封裝

以上是關於微信小程式上傳圖片到伺服器的程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)