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

news/2024/7/20 3:30:46 标签: 微信小程序, 小程序

一.下载文件

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

http://www.niftyadmin.cn/n/5282783.html

相关文章

微软官方镜像下载大全(windows iso 官方镜像)

原本只是想下一个Windows Server 2022中文版的镜像,后面发现要么就是慢得一批的某盘,要么就是磁力,我想直接下载简简单单,找了一圈没有找到。官网下载需要注册、登录乱七八糟,最终终于找到下载方法了,适用于…

react:useContent

useContent 用来简化代码 在 https://ant.design/components/button-cn 打开一个codeSanbox, 替换下面的代码保存 // import React from "react"; import React, { useContext, useState } from "react";// 创建一个名为 MyContext 的 Context…

Android Canvas画布saveLayer与对应restoreToCount,Kotlin

Android Canvas画布saveLayer与对应restoreToCount,Kotlin private fun mydraw() {val originBmp BitmapFactory.decodeResource(resources, R.mipmap.pic).copy(Bitmap.Config.ARGB_8888, true)val newBmp Bitmap.createBitmap(originBmp.width, originBmp.heigh…

屏幕颜色吸取器

前言 屏幕颜色吸取器。 前端工程师的福音,获取全屏幕上所有位置的颜色。 运行在window上的软件 屏幕颜色吸取器 前言1 下载解压2 使用 1 下载解压 下载地址:https://download.csdn.net/download/qq_44850489/11943229 下载下来之后解压 如下图&#…

智能优化算法应用:基于原子轨道搜索算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用:基于原子轨道搜索算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用:基于原子轨道搜索算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.原子轨道搜索算法4.实验参数设定…

物联网主机E6000:引领智能安防新时代

智能安防 是指利用先进的技术手段和智能化的设备,对人员、财产和环境进行全方位的监控和保护的一种安全防范系统。它集成了人工智能、物联网、云计算等技术,通过感知、识别、分析和预警等功能,实现对安全隐患的及时监测和处理。 智能安防的优…

Unity中Shader观察空间推导(在Shader中实现)

文章目录 前言一、观察空间矩阵推导1、求观察空间基向量2、求观察空间的基向量在世界空间中的矩阵 的 逆矩阵2、求平移变换矩阵3、相乘得出 观察空间转化矩阵4、得到顶点的世界空间坐标,然后转化到观察空间5、把观察空间坐标转化为齐次裁剪坐标输出到屏幕 二、最终效…

nodejs安装指导教程

1.nodejs下载 去官网下载对应的安装包即可 2.安装 下载的文件如果是exe文件直接安装,在弹出指导安装的界面有个addpath,可以将其勾选上,免得再去系统的环境变量中添加该属性。达成的效果就是,让npm命令到文件各个地方都可以被使…