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);
          },
    
        })
    },
  • 相关阅读:
    解决Windows 7 IIS7.5 用户 'IIS APPPOOL{站点名} AppPool'登录失败
    解决WebClient或HttpWebRequest首次连接缓慢问题
    VB 十六进制转汉字的函数
    xshell的常用命令
    javaweb项目添加log4j日志
    java中的事务
    eclipse中给方法加说明的快捷键
    eclipse中竖行选择代码的快捷键
    java中如何自动获取电脑的ip地址
    javaweb项目启动时自动启动rmi服务器实例
  • 原文地址:https://www.cnblogs.com/obge/p/13894962.html
Copyright © 2011-2022 走看看