小程序之 wx.downloadFile的downloadFile:fail downloadFile protocol must be http or https“ 保存图片失败

因为 uni.downloadFile 地址必须是http or https

wxfile://tmp_51ab15009cb231f65e4550c81bcbe2af5ae44ea36dc7b040.jpg 临时路径

开发者工具上chooseImage 获取到的图片路径是 http://tem
但是真机上获取到的路径不是 http://tem 类型的,而是 wxfile://tmp

真机上报错 request:fail invalid url "wxfile://tmp
真机调试失败,电脑上是可以的能下载并打开

解决办法

需要把这个文件上传到自己服务器或者第三方服务器。 因为这个临时路径随时失效的。
wx.uploadFile

uni.uploadFile({
	    url: 'https://app.cn/api/common/uploadFile', // 上传的服务器地址
	    filePath: res, // 临时文件路径
	    name: 'file', // 上传文件的字段名
	    success: function (res) {
	      this.upLoadImgUrl = JSON.parse(res.data).data.fileUrl            返回服务器的该图片地址
	      console.log(this.upLoadImgUrl, 'upImgUrl---------------')
		
		        console.log(this.upLoadImgUrl,'this.upLoadImgUrl');
			
		        that.$tools.downSaveImage(this.upLoadImgUrl)                       再调用  uni.downloadFile

	    },
	    fail: function (error) {
	      console.log(error)
	      // 处理上传失败的情况
	    },
	  })

保存方法

downSaveImage(imgurl) {

    console.log(imgurl, 'upImgUrl===========')
    uni.showModal({
      title: '保存图片',
      content: '是否保存当前图片?',
      success: (res) => {
        if (res.confirm) {
          //获取用户授权
          uni.getSetting({
            success(res) {
              if (res.authSetting['scope.writePhotosAlbum']) {
                // 已授权,直接保存图片
                uni.downloadFile({
                  // url: this.upImgUrl,
                  url: imgurl,
                  success: (res) => {
                    if (res.statusCode === 200) {
                      uni.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success: function () {
                          uni.showToast({
                            title: '保存成功',
                            duration: 2000,
                          })
                        },
                        fail: function () {
                          uni.showToast({
                            title: '保存失败,请稍后重试',
                            icon: 'none',
                          })
                        },
                      })
                    }
                  },
                })
              } else if (res.authSetting['scope.writePhotosAlbum'] === false) {
                // 用户已拒绝授权,提示用户授权
                uni.showModal({
                  title: '提示',
                  content: '您未授权保存图片到相册,是否前往设置页面进行授权?',
                  success: function (res) {
                    if (res.confirm) {
                      uni.openSetting({
                        success: function (res) {
                          if (res.authSetting['scope.writePhotosAlbum']) {
                            uni.downloadFile({
                              url: imgurl,
                              success: (res) => {
                                if (res.statusCode === 200) {
                                  uni.saveImageToPhotosAlbum({
                                    filePath: res.tempFilePath,
                                    success: function () {
                                      uni.showToast({
                                        title: '保存成功',
                                        duration: 2000,
                                      })
                                    },
                                    fail: function () {
                                      uni.showToast({
                                        title: '保存失败,请稍后重试',
                                        icon: 'none',
                                      })
                                    },
                                  })
                                }
                              },
                            })
                          }
                        },
                      })
                    } else if (res.cancel) {
                      uni.showToast({
                        title: '您取消了授权',
                        icon: 'none',
                        duration: 2000,
                      })
                    }
                  },
                })
              } else {
                // 用户第一次调用,调用授权接口
                uni.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success() {
                    uni.downloadFile({
                      url: imgurl,
                      success: (res) => {
                        if (res.statusCode === 200) {
                          uni.saveImageToPhotosAlbum({
                            filePath: res.tempFilePath,
                            success: function () {
                              uni.showToast({
                                title: '保存成功',
                                duration: 2000,
                              })
                            },
                            fail: function () {
                              uni.showToast({
                                title: '保存失败,请稍后重试',
                                icon: 'none',
                              })
                            },
                          })
                        }
                      },
                    })
                  },
                  fail() {
                    uni.showToast({
                      title: '授权失败,请稍后重试',
                      icon: 'none',
                      duration: 2000,
                    })
                  },
                })
              }
            },
          })
        } else if (res.cancel) {
          uni.showToast({
            title: '你取消了该操作',
            icon: 'none',
            duration: 2000,
          })
        }
      },
    })
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值