When making WeChat Mini Programs, you will inevitably encounter the problem of uploading files. Today I will tell you a simple example of uploading a file
wxml code
<button bindtap="upload">上传文件</button>
js code
Page({
data:{
path:''
},
upload:function(){
var that=this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
wx.uploadFile({
url: 'http://example.weixin.qq.com/upload',
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
var data = res.data
wx.showModal({
title: '上传文件返回状态',
content: '成功',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
}) //do something
},
fail:function(res){
console.log(res)
}
})
that.setData({
path:tempFilePaths
})
}
})
}
})
button The button can also be replaced with other labels, depending on the situation. In addition, I have added my own return prompt here. You can also cancel it. If you don’t understand, you can comment, or scan the QR code below.