Vue2.0中post下载

Vue2.0使用Post下载之笔记摘要!!

 
 

import axios from 'axios';
 axios({
          method: 'post',
          url: url,
          data: {参数},
          responseType: 'blob' // 告诉axios返回的数据是二进制数据
        })
        .then(response => {
           this.tableLoading=false;
          // 创建 Blob 对象
          let blob = new Blob([response.data], { type: response.headers['content-type'] });
          // 创建下载链接
          let url = window.URL.createObjectURL(blob);
          // 创建下载链接的<a>标签
          let link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', this.$uuid()+'.pdf'); // 设置下载文件名
          document.body.appendChild(link);
          // 触发点击下载
          link.click();
          // 清理
          window.URL.revokeObjectURL(url);
          document.body.removeChild(link);
          this.$message({
            type: "success",
            message: this.$t("下载成功!"),
          });
        })
        .catch(error => {
           this.tableLoading=false;
          console.error('下载失败:', error);
        });

 

posted @ 2025-05-01 22:58  小鱼记忆  阅读(20)  评论(0)    收藏  举报