查看: 954|回复: 0

[技术交流] 微擎小程序自定义util.js上传图片、音频或视频并获取返回值

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-7-4 19:04:00 | 显示全部楼层 |阅读模式

1、自定义上传功能
  1. /**
  2. * 上传图片
  3. */
  4. util.upload = function(type, filepath, success, fail) {
  5.         var typeList = ['video', 'image']
  6.         if(!type || typeList.indexOf(type) == -1) {
  7.                   type = 'video'
  8.         }
  9.         console.log(filepath)
  10.         wx.uploadFile({
  11.                 filePath: filepath,
  12.                 name: 'file',
  13.                 url: util.url('utility/file/upload', {type: type}),
  14.                 success: (res) => {
  15.                         if (res.data.errno) {
  16.                                 if(fail && typeof fail == 'function') {
  17.                                         fail(res)
  18.                                 }
  19.                         } else {
  20.                                 if(success && typeof success == 'function') {
  21.                                         success(res)
  22.                                 }
  23.                         }
  24.                 }
  25.         })
复制代码
在page.js页面中定义上传函数:
  1. upload() {
  2.     var that = this;
  3.     // 选择图片
  4.     wx.chooseMedia({
  5.       count: 1,
  6.       mediaType: ['image'],
  7.       success(res) {
  8.         console.log(res)
  9.         that.setData({
  10.           photo: res.tempFiles[0].tempFilePath,
  11.           image: res.tempFiles[0].tempFilePath
  12.         })
  13.       },
  14.       fail(res) {
  15.         console.log(res)
  16.         wx.showToast({
  17.           title: '图片处理失败了',
  18.           icon: 'none'
  19.         })
  20.       }
  21.     });
  22.   },
复制代码
然后可以在函数中调用自定义上传方法。
2、返回值:

  1. attachment: "images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
  2. ext: "png"
  3. filename: "images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
  4. filesize: 8474
  5. height: 114
  6. id: "237"
  7. is_image: 1
  8. name: "9pLJIgNleyZKc5067d9ecb5dacb1191b17070591520a.png"
  9. url: "https://wx.nongyoubang.com/attachment/images/9/2024/07/WH33JdN8k8pd3m68c8z863edk1ACWc.png"
  10. width: 200
复制代码


道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部