微信小程序文件上传,图片上传

本文详细介绍了在微信小程序中实现证件照片上传的功能,包括XML布局设计与JS逻辑处理,展示了如何使用wx.chooseImage和wx.uploadFile进行图片选择与上传,并处理上传后的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:xml

<view style="margin-top: 20px" class="weui-flex">

  <view style="width: 35%;font-size: 0.7em">证件照片</view>

  <!-- <input class="_input weui-flex__item" value="{{appkey}}" /> -->

  <view class="weui-flex__item">

  <image src='{{developer_file}}' style='width:100px;height:100px'></image>

  </view>

  <view style="display:{{isSave == 'show' ? 'none':'block'}}">

  <button size='mini' bindtap="btn_img">选择照片</button>

  </view>

</view>

2:js

btn_img: function (e) { //图片上传

var that = this;

wx.chooseImage({

count: 1, // 默认9

sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有

sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有

success: function (res) {

var tempFilePaths = res.tempFilePaths;

wx.uploadFile({

url: '', //此处换上你的接口地址

filePath: tempFilePaths[0],

name: 'file',

header: {



},

formData: {

'user': 'test',

'file': tempFilePaths[0]

},

success: function (res) {

if (res.statusCode == '200') {

console.log("图片上传成功");

var jsonData = JSON.parse(res.data);

that.setData({

imageFile: tempFilePaths,

developer_file: jsonData.data

})

} else {

showSuccess('上传失败');

}

},

fail: function (res) {

console.log('fail');

},

})

}

})

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值