企业微信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)
       
      })
    },
相关推荐
a1117761 天前
汽车展厅项目 开源项目 ThreeJS
前端·开源·html
你的不安1 天前
GEE中getInfo()
javascript·云计算·gee
阳火锅1 天前
Element / AntD 官方都没做好的功能,被这个开源小插件搞定了!
前端·vue.js·面试
大阳光男孩1 天前
Uniapp+Vue3树形选择器
前端·javascript·uni-app
沙振宇1 天前
【Web】使用Vue3+PlayCanvas开发3D游戏(九)纹理视觉效果
前端·游戏·3d·纹理
前端 贾公子1 天前
uniapp中@input修改input内容不生效 | 过滤赋值无效 | 连续非法字符不更新的问题
开发语言·前端·javascript
533_1 天前
[vxe-table] 表头:点击出现输入框
android·java·javascript
写不来代码的草莓熊1 天前
el-date-picker ,自定义输入数字自动转换显示yyyy-mm-dd HH:mm:ss格式 【仅双日历 datetimerange专用】
开发语言·前端·javascript
绺年1 天前
关于 mac 使用ssh配置
前端
LDX前端校草1 天前
verdaccio数据迁移
前端