企业微信H5文件下载。

废话不多说,直接上代码。

1.判断是不是企业微信打开的

javascript 复制代码
 const ua = navigator.userAgent.toLowerCase()
if (/micromessenger/.test(ua)) {
 }

2.复制功能

javascript 复制代码
navigator.clipboard.writeText(newsUrl).then(() => {
          this.$message({
            message: '您已复制文件链接,可在浏览器打开链接下载文件。',
            type: 'success'
          })
        })

3.消息弹窗提示

offset Message 距离窗口顶部的偏移量

duration:显示时间, 毫秒。设为 0 则不会自动关闭

javascript 复制代码
 Message({ type: 'success', message: '您已复制文件链接,可在浏览器打开链接下载文件。', offset: 200, duration: 3500 })

4.完整代码

javascript 复制代码
// 文件------查看下载文件
    handlePreview(file) {
      const reqObj = { filePath: file.url, fileName: file.name }
      //   判断是不是企业微信
      const ua = navigator.userAgent.toLowerCase()
      if (/micromessenger/.test(ua)) {
        const newsUrl = 'https://' + window.location.host + '/eal-ec-base/fileApi/download?filePath=' + file.url + '&fileName=' + file.name
        navigator.clipboard.writeText(newsUrl).then(() => {
          this.$message({
            message: '您已复制文件链接,可在浏览器打开链接下载文件。',
            type: 'success'
          })
        })
         window.open(newsUrl, '_blank')
      } else {
        this.downloadInterface(reqObj, file.name)
      }
    },
javascript 复制代码
 // 文件下载
    async downloadInterface(reqObj, fileName) {
      uploadFile(reqObj).then(res => {
        this.loadingShow = false
        const resData = res.data
        // 返回格式是文件流格式
        // 在请求拿到文件流res以后,使用a标签下载
        const pdfData = resData
        const blob = new Blob([pdfData], {
          type: `${this.fileBlobType};charset=utf-8;`
        })
        var a = document.createElement('a')
        document.body.appendChild(a)
        a.style = 'display: none'
        var url = window.URL.createObjectURL(blob)
        a.href = url
        a.setAttribute('download', fileName)
        a.click()
        a.remove()
        window.URL.revokeObjectURL(url)
       
      })
    },
相关推荐
ipad协议开发20 分钟前
企业微信 iPad 协议应用机器人开发
ios·企业微信·ipad
BD_Marathon28 分钟前
关于JS和TS选择的问题
开发语言·javascript·ecmascript
Hao_Harrision33 分钟前
50天50个小项目 (React19 + Tailwindcss V4) ✨ | DrawingApp(画板组件)
前端·react.js·typescript·tailwindcss·vite7
dly_blog34 分钟前
Vite 原理与 Vue 项目实践
前端·javascript·vue.js
仅此,1 小时前
前端接收了id字段,发送给后端就变了
java·前端·javascript·spring·typescript
Lovely Ruby1 小时前
[前端] 封装一下 echart 6,发布到 npm
前端·npm·node.js
BD_Marathon1 小时前
NPM_常见命令
前端·npm·node.js
樱桃园园长1 小时前
【Three.js 实战】手势控制 3D 奢华圣诞树 —— 从粒子系统到交互实现
javascript·3d·交互
绿鸳1 小时前
12.17面试题
前端
二狗哈1 小时前
Cesium快速入门30:CMZL动画
javascript·3d·webgl·cesium·地图可视化