企业微信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)
       
      })
    },
相关推荐
独泪了无痕1 小时前
Lodash-JavaScript的实用工具库
前端·javascript
有趣的老凌1 小时前
用 Vibe Coding 搭了一个完整小程序「一定能成」
前端·javascript·后端
kyriewen11 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_233313 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
山河木马14 小时前
矩阵专题3-怎么创建投影矩阵(uProjectionMatrix)
javascript·webgl·计算机图形学
天蓝色的鱼鱼15 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷16 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花16 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷16 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全