企业微信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)
       
      })
    },
相关推荐
wuhen_n18 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
用户69371750013841 天前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦1 天前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013841 天前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水1 天前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫1 天前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll1231 天前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命1 天前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌1 天前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛1 天前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js