微信小程序向服务器上传图片的代码解析

why 114 2024-08-27

这篇文章主要介绍了微信小程序上传图片到服务器的实例代码,在文章给大家补充介绍了微信小程序上传一或多张图片 的方法,本文给大家介绍的非常详细,具有参考借鉴加载,需要的朋友可以参考下

上传图片到服务器:

1.先在前端写一个选择图片的区域来触发wx.chooseImage接口并用wx.setStorage接口把图片路径存起来。

image.png


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

-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将刚才上传的图片上传到服务器上


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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.选取图片


1

2

3

4

5

6

7

8

wx.chooseImage({

   sizeType: [], // original 原图,compressed 压缩图,默认二者都有

   sourceType: [], // album 从相册选图,camera 使用相机,默认二者都有

   success: function (res) {

    console.log(res);

    var array = res.tempFilePaths, //图片的本地文件路径列表

   }

  })


2.上传图片


1

2

3

4

5

6

7

8

9

10

11

12

13

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) {

   }

  })


二.代码示例


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

// 点击上传图片

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) {

   }

  })

 },


以上就是本文的全部内容,希望对大家的学习有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信车载小程序,开启汽车智能化新时代
下一篇:微信小程序开发中自定义 Toast 弹框的实现技巧
相关文章

 发表评论

暂时没有评论,来抢沙发吧~