admin管理员组

文章数量:1430063

I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code

const downloadHandler = (file) => {

    const a = document.createElement("a");
    a.href = file;
    a.setAttribute(`download`, file);
    a.click();
  };

I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code

const downloadHandler = (file) => {

    const a = document.createElement("a");
    a.href = file;
    a.setAttribute(`download`, file);
    a.click();
  };
Share Improve this question edited Aug 30, 2022 at 11:10 Saad Nasim Ullah asked Aug 30, 2022 at 10:57 Saad Nasim UllahSaad Nasim Ullah 411 gold badge1 silver badge5 bronze badges 2
  • Set target to _blank on the link. If it's JavaScript-initiated, use window.open. – CherryDT Commented Aug 30, 2022 at 11:01
  • See this question, I think you can do it without opening a new page. stackoverflow./questions/11620698/… – UnnamedXAer Commented Aug 30, 2022 at 11:02
Add a ment  | 

2 Answers 2

Reset to default 1
<a href="https://yourlink." target="_blank" download>fileName</a>

This will open the HTML file in a new tab

I remend using the download attribute for download instead of jQuery:

<a href="your_link" download> file_name </a>

This will download your file, without opening it.

本文标签: reactjsfile download in new tab in javascriptStack Overflow