利用from 表单提交,直接模拟CSS from 表达去下载文件
/*
url: 下载地址
method:get || post 表单提交方式
param: 携带参数
*/ 
export const downLoad = (url,method,param) => {
    const form = document.createElement('form');
    form.style.display = 'none';
    form.method = method || 'get';
    form.action = url;
    const download_form = document.createElement('div');
    download_form.style.display = 'none';
    document.body.appendChild(download_form);
    const frage = document.createDocumentFragment();  // 收集文档碎片
    for (const key in param) {
        if(param.hasOwnProperty(key)) {
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = key;
            input.value = param[key];
            frage.appendChild(input);
        }
    }
    form.appendChild(frage);
    download_form.appendChild(form);
    form.submit();

    document.body.removeChild(download_form);

}

  就可以直接下载任何文件了, 不需要关注自己使用的ajax 还是axios 还是JQ,以默认原生方式提交

 

相关文章:

  • 2021-08-13
  • 2022-12-23
  • 2021-09-20
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
猜你喜欢
  • 2021-10-22
  • 2021-08-06
  • 2021-06-25
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案