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, usewindow.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
2 Answers
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.