1. 程式人生 > 其它 >點選按鈕,自動觸發a標籤下載檔案的功能,動態生成a標籤

點選按鈕,自動觸發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
) } },