微信小程序之下载(下载文件tempFilePath后缀unknown)和保存视频到本地相册

926 阅读1分钟

判断是否授权    copylink(e) {        var that = this        let url = this.data.selectInfo.yyVideo.videoUrl        //若二维码未加载完毕,加个动画提高用户体验        wx.showToast({          icon: 'loading',          title: '正在保存图片',          duration: 1000        })        //判断用户是否授权"保存到相册"        wx.getSetting({          success(res) {            //没有权限,发起授权            console.log(res)            if (!res.authSetting['scope.writePhotosAlbum']) {              wx.authorize({                scope: 'scope.writePhotosAlbum',                success() { //用户允许授权,保存图片到相册                  that.savePhoto(url);                },                fail() { //用户点击拒绝授权,跳转到设置页,引导用户授权                  wx.openSetting({                    success() {                      wx.authorize({                        scope: 'scope.writePhotosAlbum',                        success() {                          that.savePhoto(url);                        }                      })                    }                  })                }              })            } else { //用户已授权,保存到相册              that.savePhoto(url)            }          }        })      }      return false;    },

保存
 //保存图片到相册,提示保存成功    savePhoto(url) {      let that = this      let fileName=new Date().valueOf();      wx.downloadFile({        url: url,        filePath:wx.env.USER_DATA_PATH+'/'+fileName+'.mp4',  //防止下载文件tempFilePath后缀unknown        success: function (res) {          console.log(res)          wx.saveVideoToPhotosAlbum({            filePath: res.filePath,            success(res) {              wx.showToast({                title: '保存成功',                icon: "success",                duration: 1000              })            },            fail:function (params) {              console.log(params)            },            complete:function (s) {              console.log(s)            }          })        },              })    }