页面下载文件方法,post与get

handleExport(){
  const url='/api/admin/gateLog/export';
  const downloadHelper = $('<iframe style="display:none;" id="downloadHelper"></iframe>').appendTo('body')[0];
  const doc = downloadHelper.contentWindow.document;
  if (doc) {
    doc.open();
    doc.write('');//微软为doc.clear()有时会出bug
    doc.writeln("<html><body><form id='downloadForm' name='downloadForm' method='post' action='"+ url+"'>");
    const queryParam = this.listQuery;
    for(let key in queryParam){
    doc.writeln("<input type='hidden' name='"+key+"' value='"+queryParam[key]+"'>");
    }
    doc.writeln('<\/form><\/body><\/html>');
    doc.close();
    const form = doc.forms[0];
    if (form) {
      form.submit();
    }
  }
}
// form-data方式提交数据进行下载
// $tg表示整个当前页面的this.
function uploadFileFormData($tg,rspUrl) {
  MessageBox.confirm('是否对当前搜索条件下的数据进行导出操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    confirmButtonClass: 'importantCls roundDialog',
    cancelButtonClass: 'roundDialog',
    type: 'warning'
  }).then(() => {
    const params = Object.keys($tg.listQuery).map(key => {
      if($tg.listQuery[key] && key != 'page' && key != 'limit') {
        return encodeURIComponent(key) + "=" + encodeURIComponent($tg.listQuery[key]);
      }
    }).join("&");
    const url = rspUrl+'?token=' + getToken() + "&" + params;
    const $form = $("<form id='exportData' class='hidden' method='POST' action='" + url + "'></form>");
    $("body").append($form);
    $form.submit();
  })
}
根据url直接下载,模拟ifream下载
exportRouter() {
    this.$confirm('是否导出所有路线?', '提示', {type: 'warning'}).then(() => {
        const url = 'http://xxx.xxx/xxx'
        var iframe = document.createElement("iframe");
        iframe.src = url
        iframe.style.display = "none";
        document.body.appendChild(iframe);
        window.URL.revokeObjectURL(url); // 释放掉blob对象
    })
}

 

posted @ 2021-05-13 15:44  小二的黑豆  阅读(274)  评论(0编辑  收藏  举报