微信小程序实现图片上传

  1. data: {  //From:www.uzhanbao.com
  2.   productInfo: {}  
  3. },  
  4. //添加Banner  
  5. bindChooiceProduct: function () {  
  6.   var that = this;  
  7.   
  8.   wx.chooseImage({  
  9.     count: 3,  //最多可以选择的图片总数  
  10.     sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有  
  11.     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
  12.     success: function (res) {  
  13.       // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
  14.       var tempFilePaths = res.tempFilePaths;  
  15.       //启动上传等待中...  
  16.       wx.showToast({  
  17.         title: '正在上传...',  
  18.         icon: 'loading',  
  19.         mask: true,  
  20.         duration: 10000  
  21.       })  
  22.       var uploadImgCount = 0;  
  23.       for (var i = 0, h = tempFilePaths.length; i < h; i++) {  
  24.         wx.uploadFile({  
  25.           url:  'http://www.uzhanbao.com/home/uploadfilenew',  
  26.           filePath: tempFilePaths[i],  
  27.           name: 'uploadfile_ant',  
  28.           formData: {  
  29.             'imgIndex': i  
  30.           },  
  31.           header: {  
  32.             "Content-Type": "multipart/form-data"  
  33.           },  
  34.           success: function (res) {  
  35.             uploadImgCount++;  
  36.             var data = JSON.parse(res.data);  
  37.             //服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }  
  38.             var productInfo = that.data.productInfo;  
  39.             if (productInfo.bannerInfo == null) {  
  40.               productInfo.bannerInfo = [];  
  41.             }  
  42.             productInfo.bannerInfo.push({  
  43.               "catalog": data.Catalog,  
  44.               "fileName": data.FileName,  
  45.               "url": data.Url  
  46.             });  
  47.             that.setData({  
  48.               productInfo: productInfo  
  49.             });  
  50.   
  51.             //如果是最后一张,则隐藏等待中  
  52.             if (uploadImgCount == tempFilePaths.length) {  
  53.               wx.hideToast();  
  54.             }  
  55.           },  
  56.           fail: function (res) {  
  57.             wx.hideToast();  
  58.             wx.showModal({  
  59.               title: '错误提示',  
  60.               content: '上传图片失败',  
  61.               showCancel: false,  
  62.               success: function (res) { }  
  63.             })  
  64.           }  
  65.         });  
  66.       }  
  67.     }  
  68.   });  
  69. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值