温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何实现上传图片到服务器

发布时间:2021-07-06 09:23:15 来源:亿速云 阅读:945 作者:小新 栏目:web开发

这篇文章主要介绍了微信小程序如何实现上传图片到服务器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

上传图片到服务器:

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

微信小程序如何实现上传图片到服务器

-wxml  <view class="shangchuan" bindtap="choose">   <image  src="{{tempFilePaths}}"></image>  </view>  <button formType='submit' class="fabu">发布项目</button>  /**选择图片 */  choose: function () {   var that = this   wx.chooseImage({    count: 1,    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有    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('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: 微信小程序上传一或多张图片

一.要点

1.选取图片

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

2.上传图片

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

二.代码示例

// 点击上传图片 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]) */  /*上传多张(遍历数组,一次传一张)     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'    }, // 设置请求的 header    formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 请求中其他额外的 form data    success: function (res) {     wx.showToast({        title: "图片修改成功",        icon: 'success',        duration: 700       })    },    fail: function (res) {    }   })  },

感谢你能够认真阅读完这篇文章,希望小编分享的“微信小程序如何实现上传图片到服务器”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI