首页 小程序 微信小程序chooseMedia应用

微信小程序chooseMedia应用

用chooseMedia试着做了选择图片上传,最后我运行选择图片不管选第几张上传到存储的图片都是同一张,最后发现自己的代码出现了错误,代码中遍历了res.tempFiles,文件路径错误导致

  1. choose(){
  2. const that = this
  3. if(that.data.islog){
  4. wx.chooseMedia({
  5. count: 9,
  6. mediaType: ['image','video'],
  7. sourceType: ['album', 'camera'],
  8. maxDuration: 30,
  9. camera: 'back',
  10. success(res) {
  11. console.log(res.tempFiles)
  12. console.log(res)
  13. if(res.type == "video"){
  14. let src = res.tempFiles[0].tempFilePath.split('tmp/')[1]
  15. console.log(src)
  16. wx.cloud.uploadFile({
  17. cloudPath: 'pyq'+ src ,
  18. filePath: res.tempFiles[0].tempFilePath, // 文件路径
  19. }).then(res => {
  20. that.setData({
  21. videourl:res.fileID
  22. })
  23. console.log(that.data.videourl)
  24. that.setData({
  25. isvideo : true
  26. })
  27. }).catch(error => {
  28. // handle error
  29. })
  30. }else{
  31. res.tempFiles.forEach(function(item){
  32. console.log(item)
  33. const path = item.tempFilePath
  34. let src = item.tempFilePath.split('tmp/')[1]
  35. console.log(src)
  36. wx.cloud.uploadFile({
  37. cloudPath: 'pyq'+ src ,
  38. filePath: path, // 文件路径
  39. }).then(res => {
  40. console.log(res.fileID)
  41. that.data.src.push(res.fileID)
  42. that.setData({
  43. src:that.data.src
  44. })
  45. console.log(that.data.src)
  46. }).catch(error => {
  47. // handle error
  48. })
  49. })
  50. }
  51. }
  52. })
  53. }else{
  54. wx.showToast({
  55. title: '请先登录!',
  56. })
  57. }
  58. }
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。