小程序图片上传服务器的方法

//wxml

< view class= "release_img">
< view class= "weui-uploader__input-box">
< text class= "weui-uploader__input" bindtap= "chooseImage">+ </ text >
</ view >
< view class= "weui-uploader__files" id= "uploaderFiles">
< block wx:for= "{{images}}" wx:key= "*this">
< view class= "weui-uploader__file" bindtap= "previewImage" id= "{{item}}">
< image class= "weui-uploader__img" src= "{{item}}" mode= "aspectFill" / >
</ view >
</ block >
</ view >
</ view >


//js代码

chooseImage: function (e) {
var that = this;
var arr = [];
wx.chooseImage({
count: 3, // 默认9
sizeType: [ 'original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: [ 'album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
console.log(res);
wx.showLoading({
title: '图片正在上传',
})
setTimeout( function () {
wx.hideLoading()
}, 6000)
var tempFilePaths = res.tempFilePaths
that.setData({
images: that.data.images.concat(tempFilePaths)
})
if (that.data.images.length > 0) {
function uploadimg(data) {
var that = this;
var i = data.i ? data.i : 0, //要上传的图片
success = data.success ? data.success : 0, //上传成功的个数
fail = data.fail ? data.fail : 0; //上传失败的个数
wx.uploadFile({
url: data.url, //开发者服务器 url
filePath: data.path[i],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res);
var tupian = JSON.parse(res.data);
pics.push(tupian.data[ 0])
console.log(pics);
success++;

},
fail: function () {
fail++;
},
complete: function () {
i++;
if (i == data.path.length) {
console.log( "success:" + success + "fail" + fail);
} else {
data.i = i;
data.success = success;
data.fail = fail;
uploadimg(data);
}
}
})

}
uploadimg({
url: that.data.upImgUrl, //这里是你图片上传的接口
path: that.data.images //这里是选取的图片的地址数组
});

} else {
console.log( "没有可上传的文件");
}
}
})


},
previewImage: function (e) {
wx.previewImage({
current: e.currentTarget.id, // 当前显示图片的http链接
urls: this.data.images // 需要预览的图片http链接列表
})
},

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值