zoukankan      html  css  js  c++  java
  • 小程序 ------- 文件下载

     wx.downloadFile :下载文件资源到本地。客户端直接发起一个 HTTPS GET 请求,返回文件的本地临时路径 (本地路径),单次下载允许的最大文件为 50MB。使用前请注意阅

    注意:请在服务端响应的 header 中指定合理的 Content-Type 字段,以保证客户端正确处理文件类型。

    wx.saveImageToPhotosAlbum :保存图片到系统相册。
     wx.saveFile :保存文件到本地。注意:saveFile 会把临时文件移动,因此调用成功后传入的 tempFilePath 将不可用
     wx.openDocument:新开页面打开文档。微信客户端 7.0.12 版本前默认显示右上角菜单按钮,之后的版本默认不显示,需主动传入 showMenu
    handlerdownFile(evt){
       // console.log(evt.currentTarget.dataset)
        const filePathName = evt.currentTarget.dataset.name;
    
          //console.log(this.data.arrStudyZiLiao[0].Ext02)
      // let filePathNameLenght = filePathName.length;
       // console.log(filePathNameLenght)
        let filePathNameHZ = filePathName.lastIndexOf('.')
      //  console.log(filePathNameHZ)
        let houZhui =  filePathName.substring(filePathNameHZ+1).toLowerCase();
       // console.log(houZhui)
        const imgType = "|png|gif|jpg|";
        wx.downloadFile({
          url: 'https://www.obge.com/UpLoad/Fujian/'+filePathName,
          success: function(res){
            //console.log(res);
            const tempPath=res.tempFilePath;
    
            if(imgType.indexOf(houZhui)>0){
              wx.saveImageToPhotosAlbum({      
                filePath:tempPath,
                success(res) {
                  wx.showToast({
                  title: '保存成功',
                  icon: 'success',
                  duration: 2000
                  })
                }
              })
            }else{
              //保存预览文件
              wx.saveFile({
                tempFilePath: tempPath, 
                success:function(res){
                  const savePath = res.savedFilePath;
                  wx.openDocument({
                    filePath: savePath,
                    success :function(res){
                     // console.log(res);
                      console.log('打开文档成功')
                    },
                  });
                },
                fail: function (err) {
                  console.log('保存失败:', err)
                }
              })
        
            }
    
          },
          fail: function (err) {
            console.log('下载失败:', err);
          },
    
        })
    },
  • 相关阅读:
    leetcode1137. 第 N 个泰波那契数 吴丹阳
    改变网络接口的速度和协商方式的工具miitool 和ethtool (v0.1b)
    复杂组网模式
    wrk性能测试工具使用
    miitool 和ethtool工具介绍
    个人常用工具及命令脚本:
    python实现注册登录flask框架web开发实践
    记一次 RR 与 RC 死锁问题排查
    WGCLOUD可以作为链路监测工具吗
    WGCLOUD的server启动不了问题的终极解决办法
  • 原文地址:https://www.cnblogs.com/obge/p/13894962.html
Copyright © 2011-2022 走看看