Home > Web Front-end > JS Tutorial > What are the two ways to download files in react

What are the two ways to download files in react

王林
Release: 2020-12-16 11:07:03
forward
4235 people have browsed it

What are the two ways to download files in react

The two ways to download files in react are:

Learning video sharing: react video tutorial

1, GET Type download

 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);
  };
Copy after login

2, POST type download

 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();
  }
Copy after login

The above is the detailed content of What are the two ways to download files in react. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template