【微信小程序文件预览/下载(图片/视频/文件)】

 //文件预览
  function readfile(url, name, type, cb) {
  //url-文件地址
  //name-文件名
  //type-文件类型
  //cb-回调函数
    if (type == "jpg" || type == "png" || type == "jpeg") {
      var list = [url]
      console.log(url);
      wx.previewImage({
        current: url, // 当前显示图片的http链接
        urls: list // 需要预览的图片http链接列表
      })
      return
    } else if (type == 'mp4') {
    //视频预览自定义视频播放页面
      wx.navigateTo({
        url: '/pages/filesopen/filesopen?url=' + url,
      })

    } else if (type == 'mp3') {
      wx.navigateTo({
        url: '/pages/audioplay/audioplay?url=' + url + '&name=' + name,
      })
    } else {
      wx.showLoading({
        title: '加载中'
      })

	//通过 wx.downloadFile 下载文件 生成临时存储文件
      wx.downloadFile({
        url: url,
        success: function (res) {

          const manage = wx.getFileSystemManager();
          if (res.statusCode === 200) {
            //保存临时文件
            manage.saveFile({
              tempFilePath: res.tempFilePath,
              filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
              success: function (res) {
                console.log(res);
              }
            });
            //打开文档(支持部分文件类型,详情查阅小程序开放文档-wx.openDocument)
            wx.openDocument({
              filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
              fileType: type,
              success: function (a) {
                console.log(a);
                return typeof 'function' && cb(a);
              },
              fail: function (a) {

                wx.showToast({
                  title: '文件打开失败',
                  icon: 'none',
                  duration: 2000,
                })
              }
            })
          }

        },
        fail: function (res) {
          return typeof 'function' && cb(res);
        },
        complete() {
          wx.hideLoading({
            success: (res) => {},
          })
        }
      })
    }

  }
  //文件下载
  function downLoadFile({
    path, //下载路径
    name, //文件名
    type, //文件类型()
    system //手机系统类型
  }) {


    if (type == "jpg" || type == "png" || type == "jpeg") {
      // var list = [path]
      // console.log(path);
      // wx.previewImage({
      //   current: path, // 当前显示图片的http链接
      //   urls: list // 需要预览的图片http链接列表
      // })
      downloadimg({
        path,
        name,
        type
      })

    } else
    if (type == 'mp4') {
      // wx.navigateTo({
      //   url: '/pages/filesopen/filesopen?url=' + path,
      // })
      downloadmp4({
        path,
        name
      })
    } else {
    //ios 端与 Android  存在差异 
      if (system.toUpperCase() == 'IOS') {
        wx.showModal({
          cancelText: '取消下载',
          confirmText: '打开文件',
          content: '打开文件后,点击右上角收藏到微信。',
          success(res) {
            if (res.confirm) {
              wx.showLoading({
                title: '加载中',
              })
              wx.downloadFile({
                url: path,
                success(res) {
                  console.log(res);
                  const gFileSystemManager = wx.getFileSystemManager();

                  if (res.statusCode === 200) {

                    //保存临时文件
                    gFileSystemManager.saveFile({
                      tempFilePath: res.tempFilePath,
                      filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
                      success: function (res) {
                        console.log(res);
                      }
                    });
                    wx.openDocument({
                      filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
                      fileType: type,
                      showMenu: true,
                      success: function (res) {
                        console.log(res);
                        return typeof 'function'
                        // console.log('打开文档成功')
                      },
                      fail(err) {
                        console.log(err);
                        console.log("打开失败");
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                },
                fail(err) {
                  wx.showToast({
                    title: '文件打开失败',
                    icon: 'none',
                    duration: 2000,
                  })
                },
                complete(ook) {
                  wx.hideLoading()
                }
              })

            }
          }
        })
      } else {
        wx.showLoading({
          title: '下载中'
        })
        wx.downloadFile({
          url: path,
          success(res) {
            const FileSystemManager = wx.getFileSystemManager()
            FileSystemManager.saveFile({
              tempFilePath: res.tempFilePath,
              filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
              success(res2) {
                wx.showModal({
                  title: '下载成功',
                  content: "文件已存储此路径" + res2.savedFilePath
                })

              },
              fail(err) {
                console.log(err);
                wx.showToast({
                  title: '文件打开失败',
                  icon: 'none',
                  duration: 2000,
                })
              },
              complete(ook) {
                wx.hideLoading()
              }

            })
          }
        })
      }
    }


  }
  //视频下载
  function downloadmp4({
    path,
    name
  }) {
    console.log(wx.env, "userPath")
    wx.showLoading({
      title: '正在下载',
      mask: true
    })
    wx.downloadFile({
      url: path, // 视频地址 根据自己业务修改
      filePath: wx.env.USER_DATA_PATH + '/' + name + '.mp4',
      success: (res) => {
        console.log(res)
        wx.saveVideoToPhotosAlbum({
          filePath: res.filePath,
          success: (ress) => {
            wx.showToast({
              title: '下载成功',
              icon: 'success'
            })
            //删除临时文件
            var fileManager = wx.getFileSystemManager(); //全局唯一的文件管理器
            fileManager.unlink({ //删除
              filePath: wx.env.USER_DATA_PATH + '/' + name + '.mp4',
              success: function (resf) {
                console.log('unlink', resf)
              }
            })
          },
          fail: (ress) => {
            wx.hideLoading()
            console.log(ress)
            if (ress.errMsg == 'saveVideoToPhotosAlbum:fail auth deny') {
              wx.showModal({
                title: '提示',
                content: '需要您授与xxx"相册"权限', // 提示语, 根据业务修改
                showCancel: false,
                success: function (c) {
                  //调起客户端小程序设置界面,返回用户设置的操作结果。
                  wx.openSetting({
                    success: function (settingdata) {
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: "提示",
                          content: "获取权限成功,再次点击下载即可保存",
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: "提示",
                          content: "获取权限失败,将无法保存到相册",
                          showCancel: false
                        })
                      }
                    }
                  })
                }
              })
            }
          }
        })
      },
      fail(err) {
        console.log(err);
      }
    })
  }
  //图片下载
  function downloadimg({
    path,
    name,
    type
  }) {
    console.log(wx.env, "userPath")
    wx.showLoading({
      title: '正在下载',
      mask: true
    })
    wx.downloadFile({
      url: path, // 视频地址 根据自己业务修改
      filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
      success: (res) => {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.filePath,
          success: (ress) => {
            wx.showToast({
              title: '下载成功',
              icon: 'success'
            })
            //删除临时文件
            var fileManager = wx.getFileSystemManager(); //全局唯一的文件管理器
            fileManager.unlink({ //删除
              filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
              success: function (resf) {
                console.log('unlink', resf)
              }
            })
          },
          fail: (ress) => {
            wx.hideLoading()
            console.log(ress)
            if (ress.errMsg == 'saveVideoToPhotosAlbum:fail auth deny') {
              wx.showModal({
                title: '提示',
                content: '需要您授与xxx"相册"权限', // 提示语, 根据业务修改
                showCancel: false,
                success: function (c) {
                  //调起客户端小程序设置界面,返回用户设置的操作结果。
                  wx.openSetting({
                    success: function (settingdata) {
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: "提示",
                          content: "获取权限成功,再次点击下载即可保存",
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: "提示",
                          content: "获取权限失败,将无法保存到相册",
                          showCancel: false
                        })
                      }
                    }
                  })
                }
              })
            }
          }
        })
      },
      fail(err) {
        console.log(err);
      }
    })
  }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值