小程序 ------- 文件下载


 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);
      },

    })
},

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM