欢迎光临,了解微信小程序开发,就上易用通!

独立开发微信小程序——“桌游聚乐会”项目实践上线

发布:2017-12-15 09:03浏览: 来源:网络 作者:tianshu

微信小程序上传一或多张图片

 
 
 

一.要点

1.选取图片

 

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

2.上传图片

 

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

二.代码示例

 

						
  1. // 点击上传图片
  2. upShopLogo: function () {
  3. var that = this;
  4. wx.showActionSheet({
  5. itemList: ['从相册中选择', '拍照'],
  6. itemColor: "#f7982a",
  7. success: function (res) {
  8. if (!res.cancel) {
  9. if (res.tapIndex == 0) {
  10. that.chooseWxImageShop('album')
  11. } else if (res.tapIndex == 1) {
  12. that.chooseWxImageShop('camera')
  13. }
  14. }
  15. }
  16. })
  17. },
  18. chooseWxImageShop: function (type) {
  19. var that = this;
  20. wx.chooseImage({
  21. sizeType: ['original', 'compressed'],
  22. sourceType: [type],
  23. success: function (res) {
  24.  
  25. /*上传单张
  26. that.data.orderDetail.shopImage = res.tempFilePaths[0],
  27. that.upload_file(API_URL + 'shop/shopIcon', res.tempFilePaths[0])
  28. */
  29.  
  30. /*上传多张(遍历数组,一次传一张)
  31. for (var index in res.tempFilePaths) {
  32. that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])
  33. }
  34. */
  35. }
  36. })
  37. },
  38. upload_file: function (url, filePath) {
  39. var that = this;
  40. wx.uploadFile({
  41. url: url,
  42. filePath: filePath,
  43. name: 'uploadFile',
  44. header: {
  45. 'content-type': 'multipart/form-data'
  46. }, // 设置请求的 header
  47. formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 请求中其他额外的 form data
  48. success: function (res) {
  49. wx.showToast({
  50. title: "图片修改成功",
  51. icon: 'success',
  52. duration: 700
  53. })
  54. },
  55. fail: function (res) {
  56. }
  57. })
  58. },





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。