微信小程序 文件下载、打开、转发

一.下载文件

 wx.downloadFile({
      url: 'https://img.haihaina.cn/月度支出表.xls', 
      filePath: wx.env.USER_DATA_PATH + '/'+ '自定义名字.xlsx',
      success(res) {
        console.log('downloadFile',res)
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          fileType: 'xlsx',
          success: function(ress) {
            console.log('打开文档成功',ress)
          },
          fail: function(err) {
            console.log('保存失败:', err)
          }
        })

二.转发文件

  这里 wx.shareFileMessage只能在真机上才能进行调试,否则报错。

    wx.downloadFile({
      url: 'https://img.haihaina.cn/数据.xls', // 下载url
      success (res) {
        console.log('ressss',res)
        if(res.statusCode == 200) {
          console.log('res.statusCode',res)
            // that.openFileEvs(res)
            wx.shareFileMessage({
              filePath: res.tempFilePath,
              success(data) {
                console.log('转发成功!!!',data)
              },
              fileName:'自定义文件名字.xlsx',
              fail: console.error,
            })
        }
      },
      fileName:'导出病例',
      fail: console.error,
    })

三.下载、转发文件

    wx.downloadFile({
      url: url, // 下载url
      filePath: wx.env.USER_DATA_PATH+ '自定义文件名字.xlsx',
      success (res) {
        console.log('ressss',res)
        if(res.statusCode == 200) {
          console.log('res.statusCode',res)
            // that.openFileEvs(res)
            wx.shareFileMessage({
              filePath: res.tempFilePath,
                fileType: 'xlsx',
              success(data) {
                console.log('转发成功!!!',data)
              },
              fileName:'自定义文件名字.xlsx',
              fail: console.error,
            })
        }
      },
      fileName:'导出病例',
      fail: console.error,
    })

四.下载文件至本地,并打开文档

  打开文件苹果手机需要添加**“ fileType: fileStr,”**指定文件类型,否则无法打开;android手机可加可不加。

   downloadfile(e){
    var url = e.currentTarget.dataset.url;
    //下载文件,生成临时地址
    wx.downloadFile({
      url: url, 
      success(res) {
        // console.log(res)
        //保存到本地
        wx.saveFile({
          tempFilePath: res.tempFilePath,
          success: function (res) {
            const savedFilePath = res.savedFilePath;
            // 打开文件
            wx.openDocument({
              filePath: savedFilePath,
              //fileType: fileStr,//docx、zip、xlsx等
              success: function (res) {
                console.log('打开文档成功')
              },
            });
          },
          fail: function (err) {
            console.log('保存失败:', err)
          }
        });
      }
    })
  },

四.接收后台返回的文件流并打开

download(){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: '', //调用后台接口的全路径
      data: {memberId: this.data.member.id},
      method: "GET",
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
        'Cookie': app.globalData.userInfo && app.globalData.userInfo.cookie ? app.globalData.userInfo.cookie : '',
      },
      responseType: 'arraybuffer', //此处是请求文件流,必须带入的属性
      success: res => {
        if (res.statusCode === 200) {
          const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
          fs.writeFile({
            filePath: wx.env.USER_DATA_PATH + "/身体成分报告.pdf", // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
            data: res.data,
            encoding: "binary", //二进制流文件必须是 binary
            success (res){
              wx.openDocument({ // 打开文档
                filePath: wx.env.USER_DATA_PATH + "/身体成分报告.pdf",  //拿上面存入的文件路径
                showMenu: true, // 显示右上角菜单
                success: function (res) {
                  setTimeout(()=>{wx.hideLoading()},500)
                }
              })
            }
          })
        }
      }
    })
  }
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序文件上传与下载是指在微信小程序中进行文件的传输和获取操作。开发者可以通过微信小程序的API,实现文件上传和下载的功能,让用户能够方便地上传和下载文件。 对于文件上传功能,开发者可以使用微信小程序提供的API wx.uploadFile,通过传入服务器地址、文件路径等参数,实现将文件发送到服务器的操作。用户可以通过选择或拍摄照片、录制视频等方式,将文件上传到服务器上。在上传过程中,可以使用wx.showLoading等API对上传过程进行提示,让用户了解文件上传的进程。 对于文件下载功能,开发者可以使用微信小程序提供的API wx.downloadFile,通过传入服务器地址等参数,实现从服务器获取文件的操作。用户可以通过点击或请求的方式,将文件从服务器上下载到本地。在下载过程中,也可以使用wx.showLoading等API对下载过程进行提示,让用户了解文件下载的进程。 在使用文件上传和下载功能时,开发者需要注意文件大小和类型的限制,以及网络传输的稳定性。同时,为了提升用户体验,可以通过合理的加载动画、进度条等方式,增加用户对文件上传和下载的可视化反馈,在操作过程中提供良好的用户交互体验。 总之,微信小程序文件上传与下载功能的实现,为用户提供了方便的文件传输和获取方式,可以满足用户对文件操作的需求,提升了微信小程序的功能性和实用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值