微擎小程序自定义util.js上传图片、音频或视频并获取返回值
1、自定义上传功能
/**
* 上传图片
*/
util.upload = function(type, filepath, success, fail) {
var typeList = ['video', 'image']
if(!type || typeList.indexOf(type) == -1) {
type = 'video'
}
console.log(filepath)
wx.uploadFile({
filePath: filepath,
name: 'file',
url: util.url('utility/file/upload', {type: type}),
success: (res) => {
if (res.data.errno) {
if(fail && typeof fail == 'function') {
fail(res)
}
} else {
if(success && typeof success == 'function') {
success(res)
}
}
}
})在page.js页面中定义上传函数:
upload() {
var that = this;
// 选择图片
wx.chooseMedia({
count: 1,
mediaType: ['image'],
success(res) {
console.log(res)
that.setData({
photo: res.tempFiles.tempFilePath,
image: res.tempFiles.tempFilePath
})
},
fail(res) {
console.log(res)
wx.showToast({
title: '图片处理失败了',
icon: 'none'
})
}
});
},然后可以在函数中调用自定义上传方法。
2、返回值:
attachment: "images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
ext: "png"
filename: "images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
filesize: 8474
height: 114
id: "237"
is_image: 1
name: "9pLJIgNleyZKc5067d9ecb5dacb1191b17070591520a.png"
url: "https://wx.nongyoubang.com/attachment/images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
width: 200
页:
[1]