点击按钮,自动触发a标签下载文件的功能,动态生成a标签

<div class="model2" @click.stop="handleFileDownload">
            <i class="el-icon-download" />
            <span>点击下载查看</span>
</div>
async handleFileDownload() {
      // 点击下载查看按钮时,根据文件的ossFileId或者fileId获取文件的下载地址,然后再打开文件
      if (this.fileObj.ossFileId || this.fileObj.fileId) {
        try {
          const id = this.fileObj.ossFileId || this.fileObj.fileId
          const { resultCode, content, errMsg } = await getFileDownloadUrl(id)
          if (resultCode === 2000) {
            const myHref = document.createElement('a')
            myHref.href = content
            debounce(myHref.click(), 100)
          } else {
            this.$message({
              message: errMsg || '获取文件下载地址出错,请重试',
              type: 'error'
            })
          }
        } catch (e) {
          console.log(e)
        }
      } else if (this.fileObj.fileOssResponse) {
        const myHrefs = document.createElement('a')
        myHrefs.href = this.fileObj.fileOssResponse.downloadUrl
        debounce(myHrefs.click(), 100)
      }
 },

 

posted @ 2022-04-22 18:27  博客天天写  阅读(430)  评论(0编辑  收藏  举报