首页 > 其他分享 >导出文件 1.post请求 2.get请求

导出文件 1.post请求 2.get请求

时间:2023-01-12 16:25:58浏览次数:45  
标签:请求 get elemIF fileurls link blob downiframe post document

1.post形式
封装请求

export function postExport(url, data = {}) {
  let token = document.cookie.split('=')[1]
  return new Promise((resolve, reject) => {
    axios.post(url, JSON.stringify(data), {
      headers: {
        "Content-Type": "application/json;charset=utf-8",
        'Token': token
      },
      responseType: 'blob', //关键
    })
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
this.$postExport(exportPlateNumberImg, data).then((res) => {
        console.log(res, 'res')
        const link = document.createElement('a')
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        link.download = '车牌号画像列表.xlsx'//设置文件名--用blob方式的话,后台设置的文件名就不起作用了
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })

2.get方式

        tableExport(fileurls) {
            console.log('fileurls', fileurls)
            if (fileurls == "" || typeof fileurls == "unedfined") {
                return;
            }

            var downiframe = document.querySelector("#downiframe");
            if (downiframe) {
                downiframe.parentNode.removeChild(downiframe);
            }
            try {
                var elemIF = document.createElement("iframe");
                elemIF.id = "downiframe";
                elemIF.src = fileurls;
                elemIF.style.display = "none";
                document.body.appendChild(elemIF);
            } catch (e) {
                console.dir("error");
            }
        },
 this.tableExport(
          url +
            '?billCode=' +
            billCode +
            '&type=' +
            type +
            '&operator=' +
            operator +
            '&plateNumber=' +
            plateNumber
        )

标签:请求,get,elemIF,fileurls,link,blob,downiframe,post,document
From: https://www.cnblogs.com/hxy--Tina/p/17046959.html

相关文章