React下载文件的两种方式

React下载文件的两种方式 - 代码先锋网

不知道有用没用看着挺整齐  没试过

1、GET类型下载

 download = url => {
    const eleLink = document.createElement('a');
    eleLink.style.display = 'none';
    // eleLink.target = "_blank"
    eleLink.href = url;
    // eleLink.href = record;
    document.body.appendChild(eleLink);
    eleLink.click();
    document.body.removeChild(eleLink);
  };

2、POST类型下载

 static async download(params) {
    let form = document.createElement('form');
    form.style.display = 'none';
    form.action = `${api}tCmPaymentOrd/export`;
    form.method = 'POST';
    document.body.appendChild(form);
    // 动态创建input并给value赋值
    for (var key in params) {
      var input = document.createElement('input');
      input.type = 'hidden';
      input.name = key;
      input.value = params[key];
      form.appendChild(input);
    }

    form.submit();
    form.remove();
  }

你可能感兴趣的:(javascript,前端,html)